以编程方式将网站添加到c#中的IE11兼容性视图

时间:2015-11-16 20:39:37

标签: c# .net registry internet-explorer-11 ie-compatibility-mode

我正在尝试创建一个注册表项,将两个网站添加到IE11s兼容性视图中,如this question中所述:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData

关键UserFilter是类型REG_BINARY,但是当您查看该键或将其导出时,它似乎是一个十六进制字符串。例如,当我手动添加" example1.com"和" example2.com"到列表,然后导出密钥,这是它的内容:

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData]
    "UserFilter"=hex:41,1f,00,00,53,08,ad,ba,02,00,00,00,60,00,00,00,01,00,00,00,\
  02,00,00,00,0c,00,00,00,4f,af,fc,87,ab,20,d1,01,01,00,00,00,0c,00,65,00,78,\
  00,61,00,6d,00,70,00,6c,00,65,00,31,00,2e,00,63,00,6f,00,6d,00,0c,00,00,00,\
  cf,52,f5,89,ab,20,d1,01,01,00,00,00,0c,00,65,00,78,00,61,00,6d,00,70,00,6c,\
  00,65,00,32,00,2e,00,63,00,6f,00,6d,00

我正在尝试在c#中创建此密钥,但这样做有很多麻烦。这是我到目前为止所尝试的:

RegistryKey regKey1 = default(RegistryKey);
regKey1 = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\BrowserEmulation\\ClearableListData", true);
string hexString = @"41,1f,00,00,53,08"... etc from above
var byteArr = ConvertToByteArray(hexString, Encoding.Default);
regKey1.SetValue("UseFilter", byteArr, RegistryValueKind.Binary);
regKey1.Close(); 
//...
public static byte[] ConvertToByteArray(string str, Encoding encoding)
{
    return encoding.GetBytes(str);
}

这不起作用。它添加了一个键,但是在regedit中查看它时的值数据与上面的十六进制字符串完全不同。我也尝试过:

regKey1.SetValue("UserFilter", hexString, RegistryValueKind.Binary); // Does not work,  The type of the value object did not match the specified RegistryValueKind
regKey1.SetValue("UserFilter", hexString, RegistryValueKind.String); // Adds the key, but obviously makes it type REG_SZ and therefore does not work
regKey1.SetValue("UserFilter", hexString, RegistryValueKind.Unknown); // Does the same thing as adding a string

这是因为我在ConvertToByteArray函数上使用了错误的编码吗?我如何编写hexString有问题吗?是否有另一种方法可以将网站添加到REG_BINARY密钥?

修改

我也在ConvertToByteArray中尝试了所有不同的编码,但我遇到了和以前一样的问题 - 在regedit中查看它时的值数据与上面的十六进制字符串完全不同。

1 个答案:

答案 0 :(得分:0)

我在这里找到答案: - Write a Stringformated Hex Block to registry in Binary value。有两个问题。

  1. 我的字符串hexString包含换行符。
  2. 我从regedit导出中复制的文本在字符之间包含逗号,但这些文本需要删除,不应包含在字节中。
  3. 以下是解决方案:

    RegistryKey regKey1 = default(RegistryKey);
    regKey1 = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\BrowserEmulation\\ClearableListData", true);
    string hexString = "41,1f,00,00,53,08" + //next line... etc from above
    var data = hexString.Split(',').Select(x => Convert.ToByte(x, 16)).ToArray();
    regKey1.SetValue("UserFilter", data, RegistryValueKind.Binary);
    regKey1.Close();