Make the WebBrowser Control give you the installed IE version rendering

Updated TnTinMN 1 Tallied Votes 3K Views Share

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.

john.knapp commented: bookmarked! +3
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
Daniel_11 0 Newbie Poster

should it be a class?

diegopego 0 Newbie Poster

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);
        }
Muhammad Kamran_1 0 Newbie Poster

I have been looking for a method. As Javascript is not working.

After applying your method still its being detected as IE7.

http://detectmybrowser.com/

Suppressing error is not just solution. As we do need the script to work as well.

cgeier 187 Junior Poster

If you are using IE11, add code for IE 11.

Case 11
    If IgnoreIDocDirective Then
        value = 11001
    Else
        value = 11000
    End If
Jim_7 0 Newbie Poster

Ok, so I've never done any registry editing before. I've always been kind of terrified of it, but now I'm out of options. I have 2 things.

1.) Where exactly do I put this code? I put it above "Public Class Form1" and it gave me issues.

2.) Line 58 also threw an error while debugging.

Jim_8 0 Newbie Poster

Hi Friend

That Code is awsome, Congratulations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.