Search This Blog

Sunday, November 27, 2016

Editing windows registry to Force WebBrowser control use the latest IE version installed in local machine

Here's the C# code I used for this task:

private void EditRegistryToFixIEWebBrowserCompatibility()
       {
           string installedIEVersion = string.Empty;
           int hostAppKeyValue = 0;
           //name of the application that hosts the WebBrowser object being invoked. If you are in debugging mode, the extension should be ".vshost.exe" instead of ".exe".
           var hostAppKeyName = Assembly.GetExecutingAssembly().GetName().Name + ".exe";

           //registry key for Feature Browser Emulation
           RegistryKey regKeyFBE = null, regKeyInstalledIEVersion = null, regKeyHostApp = null;

           try
           {
               //search for Feature Browser Emulation key in relevant windows architecture
               if (Environment.Is64BitOperatingSystem)
               {
                   regKeyFBE = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION"true);
                   regKeyInstalledIEVersion = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer"true);
               }
               else
               {
                   //For 32 bit machine
                   regKeyFBE = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION"true);
                   regKeyInstalledIEVersion = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\"true);
               }

               //get current IE installed version
               installedIEVersion = Convert.ToString(regKeyInstalledIEVersion.GetValue("svcVersion"))?.Split('.').FirstOrDefault();
               int.TryParse(installedIEVersion, out hostAppKeyValue);
               hostAppKeyValue *= 1000;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Registry creation of Feature Browser Emulation key failed for Internet Explorer failed. {0}", ex.Message);
               return;
           }

           #region Search for registry key

           try
           {
               //search for a dword with a key named like our app's exe (if its value is lower than the current installed IE version, discard
               hostAppKeyValue = Math.Max(hostAppKeyValue, Convert.ToInt32(regKeyFBE.GetValue(hostAppKeyName)));
           }
           catch (Exception)
           {
               MessageBox.Show($"{hostAppKeyName} key does not exist. Creating...");
               regKeyHostApp = regKeyFBE.CreateSubKey(hostAppKeyName);
               int.TryParse(installedIEVersion, out hostAppKeyValue);
               hostAppKeyValue *= 1000;
           }

           #endregion

           // Need to set the minimum IE version currently supported by Google Maps's api - multiplied by 1000
           regKeyFBE.SetValue(hostAppKeyName, unchecked(hostAppKeyValue), RegistryValueKind.DWord);

           try
           {
               //Check for the key after adding
               hostAppKeyValue = Convert.ToInt32(regKeyFBE.GetValue(hostAppKeyName));
           }
           catch (Exception ex)
           {
               MessageBox.Show("Registry creation of Feature Browser Emulation key failed for Internet Explorer failed. {0}", ex.Message);
               return;
           }
       }

1 comment: