Monday 3 April 2017

.NET's WebBrowser control is surprisingly configurable in the registry

So currently experimenting with .NET's WebBrowser control to see if we can build user interfaces in HTML and skip Winforms, WPF, Silverlight, ActiveX and any other proprietary GUI technology that Microsoft introduces and then withdraws at a later date.  Seriously, keep your GUI standards open is the advice.

So if we add a WebBrowser control to a WinForm then it emulates Internet Explorer, but which version, the answer is its configurable.  Here is a good link for the Browser Emulation setting and on the same page are other settings. It is configurable in the registry by writing the name of your executable/assembly as the value name.

If you perhaps might change the name of your executable then you'd might like some code to write the code automatically, here is it.  So it uses Reflection to get the executable name and then writes to Registry to say use IE11 for WebBrowser control instances.

    // Make WebBrowser control emulate IE11
    RegistryKey keyIeFeatureControl = Registry.LocalMachine.OpenSubKey(
            @"Software\Microsoft\Internet Explorer\Main\FeatureControl");

    RegistryKey keyIeFeatureBrowserEmulation = keyIeFeatureControl.
            OpenSubKey("FEATURE_BROWSER_EMULATION", true);

    string exeName=Assembly.GetExecutingAssembly().GetName().Name;
    keyIeFeatureBrowserEmulation.SetValue(
            exeName, 69632); //69532=&h11000

    keyIeFeatureBrowserEmulation.Close();
    keyIeFeatureControl.Close();


Your code will need admin rights or some sort of elevated privileges to run this code otherwise you will get a security exception.

No comments:

Post a Comment