1,105,644 Community Members

Make the WebBrowser Control give you the installed IE version rendering

Member Avatar
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
1
 

I am posting this in response to those who ask about replacing the webbrowser (WB) control’s default browser. You cannot ask the WB control to use a different browser, it is hardwired to use the current version of Microsoft’s Internet Explorer that is installed on the target computer.

It may appear that the WB control is using an older version than is installed; this is not the case. Microsoft in their infinite wisdom made the decision to make IE 7 the default-rendering mode for all applications that use the WB control. This can be observed by visiting this website:

http://detectmybrowser.com

This can be overridden by adding the application name to the registry key:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Doing this can be a royal pain in the butt. Especially when developing a program as you would need to make one entry for “ApplicationName.exe” and one for “ApplicationName.vshost.exe” (for debugging under VS).

This code adds and deletes the registry entry on the fly for you. Just add it to your project. That is all that needs to be done. It automatically makes the registry changes for you.

Disclaimer: This code makes changes to the registry on Windows based systems. This is an inherently dangerous operation. The author assumes no responsibity for issues related to your use of this code. Use at your own risk.

Namespace My
   Partial Friend Class MyApplication

      Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
         CreateBrowserKey()
      End Sub

      Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
         ' I don't like applications that defecate in the registry and then don't cleanup their own mess
         ' so remove the key
         RemoveBrowerKey()
      End Sub

      Private Const BrowserKeyPath As String = "\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"

      Private Sub CreateBrowserKey(Optional ByVal IgnoreIDocDirective As Boolean = False)
         Dim basekey As String = Microsoft.Win32.Registry.CurrentUser.ToString
         Dim value As Int32
         Dim thisAppsName As String = My.Application.Info.AssemblyName & ".exe"

         ' Value reference: http://msdn.microsoft.com/en-us/library/ee330730%28v=VS.85%29.aspx
         ' IDOC Reference:  http://msdn.microsoft.com/en-us/library/ms535242%28v=vs.85%29.aspx
         Select Case (New WebBrowser).Version.Major
            Case 8
               If IgnoreIDocDirective Then
                  value = 8888
               Else
                  value = 8000
               End If

            Case 9
               If IgnoreIDocDirective Then
                  value = 9999
               Else
                  value = 9000
               End If
            Case 10
               If IgnoreIDocDirective Then
                  value = 10001
               Else
                  value = 10000
               End If

            Case Else
               Exit Sub

         End Select

         Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString & BrowserKeyPath, _
                                           Process.GetCurrentProcess.ProcessName & ".exe", _
                                           value, _
                                           Microsoft.Win32.RegistryValueKind.DWord)
      End Sub

      Private Sub RemoveBrowerKey()
         Dim key As Microsoft.Win32.RegistryKey
         key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), True)
         key.DeleteValue(Process.GetCurrentProcess.ProcessName & ".exe", False)
      End Sub
   End Class 'MyApplication

End Namespace 'My
Member Avatar
Daniel_11
Newbie Poster
1 post since Sep 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

should it be a class?

Member Avatar
diegopego
Newbie Poster
1 post since Apr 2014
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

works like a charm. here is c# version:

static string BrowserKeyPath = @"\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
        //32bit OS
        //\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
        //32bit app on 64bit OS
        //\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
        public static void RemoveBrowserKey()
        {
            RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), true);
            key.DeleteValue(Process.GetCurrentProcess().ProcessName + ".exe", false);

        }
        public static void CreateBrowserKey(bool IgnoreIDocDirective = false)
        {
            string basekey = Microsoft.Win32.Registry.CurrentUser.ToString();
            Int32 value = 11000;
            //Value reference: http://msdn.microsoft.com/en-us/library/ee330730%28v=VS.85%29.aspx
            //IDOC Reference:  http://msdn.microsoft.com/en-us/library/ms535242%28v=vs.85%29.aspx
            WebBrowser browser = new WebBrowser();

            switch (browser.Version.Major)
            {
                case 8:
                    if (IgnoreIDocDirective)
                    {
                        value = 8888;
                    }
                    else
                    {
                        value = 8000;
                    }
                    break;
                case 9:
                    if (IgnoreIDocDirective)
                    {
                        value = 9999;
                    }
                    else
                    {
                        value = 9000;
                    }
                    break;
                case 10:
                    if (IgnoreIDocDirective)
                    {
                        value = 10001;
                    }
                    else
                    {
                        value = 10000;
                    }
                    break;
                case 11:
                    if (IgnoreIDocDirective)
                    {
                        value = 11001;
                    }
                    else
                    {
                        value = 11000;
                    }
                    break;
                default:
                    break;
            }

            Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString() + BrowserKeyPath, Process.GetCurrentProcess().ProcessName + ".exe", value, Microsoft.Win32.RegistryValueKind.DWord);
        }
You
Post:
Start New Discussion
View similar articles that have also been tagged: