enphit
Everything of dotnet!
随笔- 7  文章- 0  评论- 6 
博客园  首页  新随笔  联系  管理  订阅 订阅
2007年7月12日
从剪贴板读取数据乱码问题

以前用这种方读取剪贴板的内容怎么都会有点点乱码,在网上查了很久,终于找到了解决的办法。

 1if (iData.GetDataPresent(DataFormats.Html))
 2            {
 3                string d = iData.GetData(DataFormats.Html).ToString();
 4                textBox1.Text = d;
 5                if (listBox1.SelectedItem != null && listBox2.SelectedItem != null)
 6                {
 7                    byte[] b = Encoding.GetEncoding(listBox1.SelectedItem.ToString()).GetBytes(textBox1.Text);
 8                    textBox2.Text = Encoding.GetEncoding(listBox2.SelectedItem.ToString()).GetString(b);
 9                }

10            }
解决办法:
1if (Clipboard.ContainsText(TextDataFormat.Html))
2{
3    MemoryStream vMemoryStream = 
4        Clipboard.GetData("Html Format") as MemoryStream;
5    vMemoryStream.Position = 0;
6    byte[] vBytes = new byte[vMemoryStream.Length];
7    vMemoryStream.Read(vBytes, 0, (int)vMemoryStream.Length);
8    textBox1.Text = Encoding.UTF8.GetString(vBytes);
9}
高兴ing.
posted @ 2007-07-12 15:54 机枪兵 阅读(574) 评论(1) 编辑
Copyright ©2012 机枪兵