Use Latest Version of Internet Explorer in the Webbrowser Control

Use latest version of Internet Explorer in the webbrowser control

I saw Veer's answer. I think it's right, but it did not I work for me. Maybe I am using .NET 4 and am using 64x OS so kindly check this.

You may put in setup or check it in start-up of your application:

private void Form1_Load(object sender, EventArgs e)
{
var appName = Process.GetCurrentProcess().ProcessName + ".exe";
SetIE8KeyforWebBrowserControl(appName);
}

private void SetIE8KeyforWebBrowserControl(string appName)
{
RegistryKey Regkey = null;
try
{
// For 64 bit machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit machine
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);

// If the path is not correct or
// if the user haven't priviledges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application Settings Failed - Address Not found");
return;
}

string FindAppkey = Convert.ToString(Regkey.GetValue(appName));

// Check if key is already present
if (FindAppkey == "8000")
{
MessageBox.Show("Required Application Settings Present");
Regkey.Close();
return;
}

// If a key is not present add the key, Key value 8000 (decimal)
if (string.IsNullOrEmpty(FindAppkey))
Regkey.SetValue(appName, unchecked((int)0x1F40), RegistryValueKind.DWord);

// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));

if (FindAppkey == "8000")
MessageBox.Show("Application Settings Applied Successfully");
else
MessageBox.Show("Application Settings Failed, Ref: " + FindAppkey);
}
catch (Exception ex)
{
MessageBox.Show("Application Settings Failed");
MessageBox.Show(ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}

You may find messagebox.show, just for testing.

Keys are as the following:

  • 11001 (0x2AF9) - Internet Explorer 11. Webpages are displayed in IE11
    edge mode, regardless of the !DOCTYPE directive.

  • 11000 (0x2AF8) - Internet Explorer 11. Webpages containing
    standards-based !DOCTYPE directives are displayed in IE11 edge mode.
    Default value for IE11.

  • 10001 (0x2711)- Internet Explorer 10. Webpages are displayed in IE10 Standards
    mode, regardless of the !DOCTYPE directive.

  • 10000 (0x2710)- Internet Explorer 10. Webpages containing standards-based
    !DOCTYPE directives are displayed in IE10 Standards mode. Default
    value for Internet Explorer 10.

  • 9999 (0x270F) - Internet Explorer 9. Webpages are displayed in IE9
    Standards mode, regardless of the !DOCTYPE directive.

  • 9000 (0x2328) - Internet Explorer 9. Webpages containing
    standards-based !DOCTYPE directives are displayed in IE9 mode.

  • 8888 (0x22B8) - Webpages are displayed in IE8 Standards mode,
    regardless of the !DOCTYPE directive.

  • 8000 (0x1F40) - Webpages containing standards-based !DOCTYPE
    directives are displayed in IE8 mode.

  • 7000 (0x1B58) - Webpages containing standards-based !DOCTYPE
    directives are displayed in IE7 Standards mode.

Reference: MSDN: Internet Feature Controls

I saw applications like Skype use 10001. I do not know.

NOTE

The setup application will change the registry. You may need to add a line in the Manifest File to avoid errors due to permissions of change in registry:

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

UPDATE 1

This is a class will get the latest version of IE on windows and make changes as should be;

public class WebBrowserHelper
{


public static int GetEmbVersion()
{
int ieVer = GetBrowserVersion();

if (ieVer > 9)
return ieVer * 1000 + 1;

if (ieVer > 7)
return ieVer * 1111;

return 7000;
} // End Function GetEmbVersion

public static void FixBrowserVersion()
{
string appName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
FixBrowserVersion(appName);
}

public static void FixBrowserVersion(string appName)
{
FixBrowserVersion(appName, GetEmbVersion());
} // End Sub FixBrowserVersion

// FixBrowserVersion("<YourAppName>", 9000);
public static void FixBrowserVersion(string appName, int ieVer)
{
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", ieVer);
} // End Sub FixBrowserVersion

private static void FixBrowserVersion_Internal(string root, string appName, int ieVer)
{
try
{
//For 64 bit Machine
if (Environment.Is64BitOperatingSystem)
Microsoft.Win32.Registry.SetValue(root + @"\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);
else //For 32 bit Machine
Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);


}
catch (Exception)
{
// some config will hit access rights exceptions
// this is why we try with both LOCAL_MACHINE and CURRENT_USER
}
} // End Sub FixBrowserVersion_Internal

public static int GetBrowserVersion()
{
// string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };

int maxVer = 0;
for (int i = 0; i < ls.Length; ++i)
{
object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
string strVal = System.Convert.ToString(objVal);
if (strVal != null)
{
int iPos = strVal.IndexOf('.');
if (iPos > 0)
strVal = strVal.Substring(0, iPos);

int res = 0;
if (int.TryParse(strVal, out res))
maxVer = Math.Max(maxVer, res);
} // End if (strVal != null)

} // Next i

return maxVer;
} // End Function GetBrowserVersion


}

using of class as followed

WebBrowserHelper.FixBrowserVersion();
WebBrowserHelper.FixBrowserVersion("SomeAppName");
WebBrowserHelper.FixBrowserVersion("SomeAppName",intIeVer);

you may face a problem for in comparability of windows 10, may due to your website itself
you may need to add this meta tag

<meta http-equiv="X-UA-Compatible" content="IE=11" >

Enjoy :)

How can I get the WebBrowser control to show modern contents?

Note: The post is about WebBrowser control, however, for all the new
.NET projects the main solution is using
WebView2.
To learn more, take a look at this post:

  • Getting started with WebView2.
WebBrowser Control

The WebBrowser control uses the same Internet Explorer version which is installed on your OS but it doesn't use the latest document mode by default and shows content in compatibility mode.

Symptom - As a symptom, the site works properly in Internet Explorer or other browsers, but WebBrowser control doesn't show the site well and for some sites it shows script errors.

Solution - You can tell the WebBrowser control to use the latest document mode without compatibility mode in WebBrowser control. You can follow instructions here to disable the setting using registry.
[Reference: Browser Emulation]

Apply Browser Emulation setting using code

If you want to apply the settings using code, run the following code once:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
true))
{
var app = System.IO.Path.GetFileName(Application.ExecutablePath);
key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
}

In above code, I've used 11001 which means IE11 Edge mode.

Internet Explorer 11. Webpages are displayed in IE11 edge mode,
regardless of the declared !DOCTYPE directive. Failing to declare a
!DOCTYPE directive causes the page to load in Quirks.

Apply the Browser Emulation setting manually

Open Registry editor and browse HKEY_CURRENT_USER, go to the following key:

Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

Add the following values:

"YourApplicationFileName.exe"=dword:00002af9
"YourApplicationFileName.vshost.exe"=dword:00002af9

(In older versions of Visual Studio you needed to add vshost.exe value as well, when you run your program in Visual Studio.)

To create entries right click on an empty area of the right pane, then in the window which appears after selecting dword value, choose hexadecimal and enter 2af9:

Sample Image

In above steps, I've used 11001 which means IE11 Edge mode.

Use WebViewCompatible Control for Windows Forms

You can also use the new WebViewCompatible control for Windows Forms. You can see simple steps to use here: Replace WebBrowser control by new WebView Compatible control for Windows Forms.

WebViewCompatible uses one of two rendering engines to support a broader set of Windows clients:

  • On Windows 10 devices, the newer Microsoft Edge rendering engine is used to embed a view that renders richly formatted HTML content from a remote web server, dynamically generated code, or content files.

  • On devices running older versions of Windows, the System.Windows.Controls.WebBrowser is used, which provides Internet Explorer engine-based rendering.

  • Note: WebView2 is a replacement for WebView and WebViewCompatible.

Set X-UA-Compatibile meta tag

In case that you have access to the html content of the page and you can change the content (for example it's a local html file, or the site belong to yourself) then you can set X-UA-Compatibile meta tag in the head like: <meta http-equiv="X-UA-Compatible" content="IE=Edge" />.

Use other Browser Controls

You can rely on other browser controls like CefSharp.

Cannot force WebBrowser Control to render using current version of IE

You need to add your registry key under both the main (64bit) node and the 32bit node, HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl

You should then visit http://webdbg.com/ua.aspx to verify the document mode and UA string.

Use latest version of Internet Explorer in the webbrowser control

I saw Veer's answer. I think it's right, but it did not I work for me. Maybe I am using .NET 4 and am using 64x OS so kindly check this.

You may put in setup or check it in start-up of your application:

private void Form1_Load(object sender, EventArgs e)
{
var appName = Process.GetCurrentProcess().ProcessName + ".exe";
SetIE8KeyforWebBrowserControl(appName);
}

private void SetIE8KeyforWebBrowserControl(string appName)
{
RegistryKey Regkey = null;
try
{
// For 64 bit machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit machine
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);

// If the path is not correct or
// if the user haven't priviledges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application Settings Failed - Address Not found");
return;
}

string FindAppkey = Convert.ToString(Regkey.GetValue(appName));

// Check if key is already present
if (FindAppkey == "8000")
{
MessageBox.Show("Required Application Settings Present");
Regkey.Close();
return;
}

// If a key is not present add the key, Key value 8000 (decimal)
if (string.IsNullOrEmpty(FindAppkey))
Regkey.SetValue(appName, unchecked((int)0x1F40), RegistryValueKind.DWord);

// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));

if (FindAppkey == "8000")
MessageBox.Show("Application Settings Applied Successfully");
else
MessageBox.Show("Application Settings Failed, Ref: " + FindAppkey);
}
catch (Exception ex)
{
MessageBox.Show("Application Settings Failed");
MessageBox.Show(ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}

You may find messagebox.show, just for testing.

Keys are as the following:

  • 11001 (0x2AF9) - Internet Explorer 11. Webpages are displayed in IE11
    edge mode, regardless of the !DOCTYPE directive.

  • 11000 (0x2AF8) - Internet Explorer 11. Webpages containing
    standards-based !DOCTYPE directives are displayed in IE11 edge mode.
    Default value for IE11.

  • 10001 (0x2711)- Internet Explorer 10. Webpages are displayed in IE10 Standards
    mode, regardless of the !DOCTYPE directive.

  • 10000 (0x2710)- Internet Explorer 10. Webpages containing standards-based
    !DOCTYPE directives are displayed in IE10 Standards mode. Default
    value for Internet Explorer 10.

  • 9999 (0x270F) - Internet Explorer 9. Webpages are displayed in IE9
    Standards mode, regardless of the !DOCTYPE directive.

  • 9000 (0x2328) - Internet Explorer 9. Webpages containing
    standards-based !DOCTYPE directives are displayed in IE9 mode.

  • 8888 (0x22B8) - Webpages are displayed in IE8 Standards mode,
    regardless of the !DOCTYPE directive.

  • 8000 (0x1F40) - Webpages containing standards-based !DOCTYPE
    directives are displayed in IE8 mode.

  • 7000 (0x1B58) - Webpages containing standards-based !DOCTYPE
    directives are displayed in IE7 Standards mode.

Reference: MSDN: Internet Feature Controls

I saw applications like Skype use 10001. I do not know.

NOTE

The setup application will change the registry. You may need to add a line in the Manifest File to avoid errors due to permissions of change in registry:

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

UPDATE 1

This is a class will get the latest version of IE on windows and make changes as should be;

public class WebBrowserHelper
{


public static int GetEmbVersion()
{
int ieVer = GetBrowserVersion();

if (ieVer > 9)
return ieVer * 1000 + 1;

if (ieVer > 7)
return ieVer * 1111;

return 7000;
} // End Function GetEmbVersion

public static void FixBrowserVersion()
{
string appName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
FixBrowserVersion(appName);
}

public static void FixBrowserVersion(string appName)
{
FixBrowserVersion(appName, GetEmbVersion());
} // End Sub FixBrowserVersion

// FixBrowserVersion("<YourAppName>", 9000);
public static void FixBrowserVersion(string appName, int ieVer)
{
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", ieVer);
} // End Sub FixBrowserVersion

private static void FixBrowserVersion_Internal(string root, string appName, int ieVer)
{
try
{
//For 64 bit Machine
if (Environment.Is64BitOperatingSystem)
Microsoft.Win32.Registry.SetValue(root + @"\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);
else //For 32 bit Machine
Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);


}
catch (Exception)
{
// some config will hit access rights exceptions
// this is why we try with both LOCAL_MACHINE and CURRENT_USER
}
} // End Sub FixBrowserVersion_Internal

public static int GetBrowserVersion()
{
// string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };

int maxVer = 0;
for (int i = 0; i < ls.Length; ++i)
{
object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
string strVal = System.Convert.ToString(objVal);
if (strVal != null)
{
int iPos = strVal.IndexOf('.');
if (iPos > 0)
strVal = strVal.Substring(0, iPos);

int res = 0;
if (int.TryParse(strVal, out res))
maxVer = Math.Max(maxVer, res);
} // End if (strVal != null)

} // Next i

return maxVer;
} // End Function GetBrowserVersion


}

using of class as followed

WebBrowserHelper.FixBrowserVersion();
WebBrowserHelper.FixBrowserVersion("SomeAppName");
WebBrowserHelper.FixBrowserVersion("SomeAppName",intIeVer);

you may face a problem for in comparability of windows 10, may due to your website itself
you may need to add this meta tag

<meta http-equiv="X-UA-Compatible" content="IE=11" >

Enjoy :)



Related Topics



Leave a reply



Submit