Clipboard.GetText(TextDataFormat.Html)字符串长度小于标题EndHTML

时间:2014-05-23 01:41:28

标签: html vb.net clipboard

让一些代码在剪贴板上查看HTML。它适用于XP,但在W7上有以下问题。 HTML剪贴板标题EndHTML值大于生成的Clipboard.GetText的字符串长度。 IE11和Firefox都有同样的问题。要测试浏览到:http://stackexchange.com/

编辑全选,然后复制。以下代码段显示问题。也许与编码有关?

Dim dto As IDataObject = Clipboard.GetDataObject()
    Dim CBdata As String = ""
    Dim startHTML As String = ""
    Dim endHTML As String = ""
    If dto.GetDataPresent(DataFormats.Html) Then
        CBdata = Clipboard.GetText(TextDataFormat.Html)

        Dim m As Match = Regex.Match(CBdata, "StartHTML:(\d+)")
        If m.Success Then startHTML = m.Groups(1).Value
        m = Regex.Match(CBdata, "EndHTML:(\d+)")
        If m.Success Then endHTML = m.Groups(1).Value

        Console.WriteLine("CB data length=" & CBdata.Length.ToString)
        Console.WriteLine("EndHtml=" & endHTML)
        Console.WriteLine("StartHtml=" & startHTML)

        'To get just the html:
        'CBdata.Substring(startHTML, endHTML - startHTML)
        ' but of course throws a subscript exception

    End If

1 个答案:

答案 0 :(得分:0)

StartHTML and EndHTML计数器计算字节数,而不是字符数。因此,如果您的文本包含一个以两个字节编码的UTF8字符,则您将获得2而不是1的长度。

现在,为什么它在XP中工作,现在它不在Win 7中。我发现可能是.NET 4.5的变化: Different behavior of DataObject.GetData(DataFormats.Html) in .NET 4.5