【问题标题】:Programmatically adding a website to IE11 compatibility view in c#在 c# 中以编程方式将网站添加到 IE11 兼容性视图
【发布时间】:2015-11-16 20:39:36
【问题描述】:

我正在尝试创建一个注册表项,将两个网站添加到 IE11 的兼容性视图中,如 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 中查看值数据时与上面的十六进制字符串完全不同。

【问题讨论】:

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


    【解决方案1】:

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

    1. 我的字符串 hexString 包含换行符。
    2. 我从 regedit 导出复制的文本包含字符之间的逗号,但这些需要删除并且不应包含在字节中。

    解决办法如下:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2011-10-05
      • 2017-10-09
      相关资源
      最近更新 更多