Search code examples
c#.netregistryinternet-explorer-11ie-compatibility-mode

Programmatically adding a website to IE11 compatibility view in c#


I am attempting to create a registry key that adds two websites to IE11s compatibility view, described in this question:

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

The key UserFilter is type REG_BINARY, but when you look at the key or export it, it appears to be a hex string. For example, when I add manually "example1.com" and "example2.com" to the list, and then export the key, this is what it reads:

    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

I am trying to create this key in c#, but having a lot of trouble doing so. This is what I have tried so far:

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

This does not work. It adds a key, but the value data when looking at it in regedit is completely different than the hex string above. I have also tried:

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

Is this because I am using the wrong encoding on my ConvertToByteArray function? Is there a problem with how I am writing the hexString? Is there another way to add websites to a REG_BINARY key?

Edit:

I have also tried all of the different encodings in ConvertToByteArray, but I am having the same problem as before - the value data when looking at it in regedit is completely different than the hex string above.


Solution

  • I found the answer here: - Write a Stringformated Hex Block to registry in Binary value. There were two issues.

    1. My string hexString contained newline characters.
    2. The text I copied from regedit export contained commas between characters, but these need to be removed and shouldn't be included in the bytes.

    Here is the solution:

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