C#使用Clipboard.SetText(String, TextDataFormat.Html) 将HTML数据写入到剪切板中,英文和符号可以正常显示超链接,但是中文会显示乱码,尝试过转码,还是乱码,请问如何解决? Set的文本如下:Version:0.9
StartHTML:0000000178
EndHTML:0000000908
StartFragment:0000000216
EndFragment:0000000870
SourceURL:SourceURL:http://www.sbxinwen.com
<html>
<body>
<!--StartFragment-->
<a href="http://www.sbxinwen.com" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-size: 12px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">傻逼</a>
<!--EndFragment-->
</body>
</html>

解决方案 »

  1.   

    试了下,木有发现问题……byte[] bs = File.ReadAllBytes("index.html");
    string s = Encoding.UTF8.GetString(bs);//html文件是UTF-8编码
    Clipboard.SetText(s, TextDataFormat.Html);
    MessageBox.Show(Clipboard.GetText(TextDataFormat.Html));
      

  2.   

    你在Clipboard.SetText(String,   TextDataFormat.Html) 这里断下来看看string里面是否中文已经是乱码了
      

  3.   


    不是乱码。
    附上我的代码
     try
                        {
                            string htmldata = GetHtmlData("http://www.sbxinwen.com", "傻逼");
                            Clipboard.SetText(textBox2.Text + htmldata +textBox3.Text, TextDataFormat.Html);
                        }
                        catch (Exception ee) 
                        {
                            MessageBox.Show(ee.ToString());
                        }
    public string GetHtmlData(string url, string text)
            {
                string shuju = "<a href=\"" + url + "\" target=\"_blank\" style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-size: 12px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); \">" + text + "</a>";
                return shuju;
            }
      

  4.   

    其中textbox2的值是
    Version:0.9
    StartHTML:0000000178
    EndHTML:0000000908
    StartFragment:0000000216
    EndFragment:0000000870
    SourceURL:SourceURL:http://www.sbxinwen.com
    <html>
    <body>
    <!--StartFragment-->
    textbox3是<!--EndFragment-->
    </body>
    </html>
      

  5.   

    你把这两头的去掉看看textBox2.Text + htmldata +textBox3.Text
      

  6.   

    Version:0.9
    StartHTML:0000000178
    EndHTML:0000000908
    StartFragment:0000000216
    EndFragment:0000000870
    SourceURL:SourceURL:http://www.sbxinwen.com
    <html>
    <body>
    <!--StartFragment-->
    <a href="http://www.sbxinwen.com" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-size: 12px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">傻逼</a>
    <!--EndFragment-->
    </body>
    </html>
      

  7.   

    TextDataFormat.Html 改成 TextDataFormat.UnicodeText 试试
      

  8.   


    不行,直接就报错了************** 异常文本 **************
    System.Runtime.InteropServices.ExternalException: 所要求的剪贴板操作失败。
       在 System.Windows.Forms.Clipboard.ThrowIfFailed(Int32 hr)
       在 System.Windows.Forms.Clipboard.GetDataObject(Int32 retryTimes, Int32 retryDelay)
       在 System.Windows.Forms.Clipboard.GetDataObject()
       在 System.Windows.Forms.Clipboard.GetText(TextDataFormat format)
       在 System.Windows.Forms.Clipboard.GetText()
       在 XIAOCSTART.Form1.Dv0IhM2BOdC8IHgVNd()
       在 XIAOCSTART.Form1.8lAIwTE5O(Object , EventArgs )
       在 System.Windows.Forms.Timer.OnTick(EventArgs e)
       在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      

  9.   

    解决了。。原来我们的起步都错了!应该用SetGet而不是SetText...代码如下
    string s = textBox2.Text + htmldata + textBox3.Text;
                                MemoryStream vMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(s));
                                Clipboard.SetData(DataFormats.Html, vMemoryStream);