The default version of the webbrowser control in a C# Windows Forms application is 7. I have changed to 9 by the article Browser Emulation, but how is it possible t
I was able to implement Luca's solution, but I had to make a few changes for it to work. My goal was to use D3.js with a Web Browser control for a Windows Forms Application (targeting .NET 2.0). It is working for me now. I hope this can help someone else.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
namespace ClientUI
{
static class Program
{
static Mutex mutex = new System.Threading.Mutex(false, "jMutex");
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
// Another application instance is running
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var targetApplication = Process.GetCurrentProcess().ProcessName + ".exe";
int ie_emulation = 11999;
try
{
string tmp = Properties.Settings.Default.ie_emulation;
ie_emulation = int.Parse(tmp);
}
catch { }
SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation);
Application.Run(new MainForm());
}
finally
{
mutex.ReleaseMutex();
}
}
private static void SetIEVersioneKeyforWebBrowserControl(string appName, int ieval)
{
RegistryKey Regkey = null;
try
{
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if user doesn't have privileges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == ieval.ToString())
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION already set to " + ieval);
Regkey.Close();
return;
}
// If key is not present or different from desired, add/modify the key , key value
Regkey.SetValue(appName, unchecked((int)ieval), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == ieval.ToString())
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart");
}
else
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; current value is " + ieval);
}
}
catch (Exception ex)
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message);
}
finally
{
//Close the Registry
if (Regkey != null) Regkey.Close();
}
}
}
}
Also, I added a string (ie_emulation) to the project's settings with the value of 11999. This value seems to be working for IE11(11.0.15).
Next, I had to change the permission for my application to allow access to the registry. This can be done by adding a new item to your project (using VS2012). Under the General Items, select Application Manifest File. Change the level from asInvoker to requireAdministrator (as shown below).
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
If someone reading this is trying to use D3.js with a webbrowser control, you may have to modify the JSON data to be stored within a variable inside your HTML page because D3.json uses XmlHttpRequest (easier to use with a webserver). After those changes and the above, my windows forms are able to load local HTML files that call D3.
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 :)
Here the method that I usually use and works for me (both for 32 bit and 64 bit applications; ie_emulation can be anyone documented here: Internet Feature Controls (B..C), Browser Emulation):
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
// Another application instance is running
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var targetApplication = Process.GetCurrentProcess().ProcessName + ".exe";
int ie_emulation = 10000;
try
{
string tmp = Properties.Settings.Default.ie_emulation;
ie_emulation = int.Parse(tmp);
}
catch { }
SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation);
m_webLoader = new FormMain();
Application.Run(m_webLoader);
}
finally
{
mutex.ReleaseMutex();
}
}
private static void SetIEVersioneKeyforWebBrowserControl(string appName, int ieval)
{
RegistryKey Regkey = null;
try
{
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if user haven't privileges to access the registry
if (Regkey == null)
{
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == "" + ieval)
{
YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION already set to " + ieval);
Regkey.Close();
return;
}
// If a key is not present or different from desired, add/modify the key, key value
Regkey.SetValue(appName, unchecked((int)ieval), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == "" + ieval)
YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart");
else
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; current value is " + ieval);
}
catch (Exception ex)
{
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}
A cheap and easy workaround for it is that you can just put a value which is bigger than 11001 in the FEATURE_BROWSER_EMULATION key. Then it takes the latest IE that is available in the system.
var appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
using (var Key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true))
Key.SetValue(appName, 99999, RegistryValueKind.DWord);
According to what I read here (Controlling WebBrowser Control Compatibility:
What Happens if I Set the FEATURE_BROWSER_EMULATION Document Mode Value Higher than the IE Version on the Client?
Obviously, the browser control can only support a document mode that is less than or equal to the IE version installed on the client. Using the FEATURE_BROWSER_EMULATION key works best for enterprise line of business apps where there is a deployed and support version of the browser. In the case you set the value to a browser mode that is a higher version than the browser version installed on the client, the browser control will choose the highest document mode available.
The simplest thing is to put a very high decimal number ...
I know this has been posted but here is a current version for dotnet 4.5 above that I use. I recommend to use the default browser emulation respecting doctype
InternetExplorerFeatureControl.Instance.BrowserEmulation = DocumentMode.DefaultRespectDocType;
internal class InternetExplorerFeatureControl
{
private static readonly Lazy<InternetExplorerFeatureControl> LazyInstance = new Lazy<InternetExplorerFeatureControl>(() => new InternetExplorerFeatureControl());
private const string RegistryLocation = @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl";
private readonly RegistryView _registryView = Environment.Is64BitOperatingSystem && Environment.Is64BitProcess ? RegistryView.Registry64 : RegistryView.Registry32;
private readonly string _processName;
private readonly Version _version;
#region Feature Control Strings (A)
private const string FeatureRestrictAboutProtocolIe7 = @"FEATURE_RESTRICT_ABOUT_PROTOCOL_IE7";
private const string FeatureRestrictAboutProtocol = @"FEATURE_RESTRICT_ABOUT_PROTOCOL";
#endregion
#region Feature Control Strings (B)
private const string FeatureBrowserEmulation = @"FEATURE_BROWSER_EMULATION";
#endregion
#region Feature Control Strings (G)
private const string FeatureGpuRendering = @"FEATURE_GPU_RENDERING";
#endregion
#region Feature Control Strings (L)
private const string FeatureBlockLmzScript = @"FEATURE_BLOCK_LMZ_SCRIPT";
#endregion
internal InternetExplorerFeatureControl()
{
_processName = $"{Process.GetCurrentProcess().ProcessName}.exe";
using (var webBrowser = new WebBrowser())
_version = webBrowser.Version;
}
internal static InternetExplorerFeatureControl Instance => LazyInstance.Value;
internal RegistryHive RegistryHive { get; set; } = RegistryHive.CurrentUser;
private int GetFeatureControl(string featureControl)
{
using (var currentUser = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, _registryView))
{
using (var key = currentUser.CreateSubKey($"{RegistryLocation}\\{featureControl}", false))
{
if (key.GetValue(_processName) is int value)
{
return value;
}
return -1;
}
}
}
private void SetFeatureControl(string featureControl, int value)
{
using (var currentUser = RegistryKey.OpenBaseKey(RegistryHive, _registryView))
{
using (var key = currentUser.CreateSubKey($"{RegistryLocation}\\{featureControl}", true))
{
key.SetValue(_processName, value, RegistryValueKind.DWord);
}
}
}
#region Internet Feature Controls (A)
/// <summary>
/// Windows Internet Explorer 8 and later. When enabled, feature disables the "about:" protocol. For security reasons, applications that host the WebBrowser Control are strongly encouraged to enable this feature.
/// By default, this feature is enabled for Windows Internet Explorer and disabled for applications hosting the WebBrowser Control.To enable this feature using the registry, add the name of your executable file to the following setting.
/// </summary>
internal bool AboutProtocolRestriction
{
get
{
if (_version.Major < 8)
throw new NotSupportedException($"{AboutProtocolRestriction} requires Internet Explorer 8 and Later.");
var releaseVersion = new Version(8, 0, 6001, 18702);
return Convert.ToBoolean(GetFeatureControl(_version >= releaseVersion ? FeatureRestrictAboutProtocolIe7 : FeatureRestrictAboutProtocol));
}
set
{
if (_version.Major < 8)
throw new NotSupportedException($"{AboutProtocolRestriction} requires Internet Explorer 8 and Later.");
var releaseVersion = new Version(8, 0, 6001, 18702);
SetFeatureControl(_version >= releaseVersion ? FeatureRestrictAboutProtocolIe7 : FeatureRestrictAboutProtocol, Convert.ToInt16(value));
}
}
#endregion
#region Internet Feature Controls (B)
/// <summary>
/// Windows Internet Explorer 8 and later. Defines the default emulation mode for Internet Explorer and supports the following values.
/// </summary>
internal DocumentMode BrowserEmulation
{
get
{
if (_version.Major < 8)
throw new NotSupportedException($"{nameof(BrowserEmulation)} requires Internet Explorer 8 and Later.");
var value = GetFeatureControl(FeatureBrowserEmulation);
if (Enum.IsDefined(typeof(DocumentMode), value))
{
return (DocumentMode)value;
}
return DocumentMode.NotSet;
}
set
{
if (_version.Major < 8)
throw new NotSupportedException($"{nameof(BrowserEmulation)} requires Internet Explorer 8 and Later.");
var tmp = value;
if (value == DocumentMode.DefaultRespectDocType)
tmp = DefaultRespectDocType;
else if (value == DocumentMode.DefaultOverrideDocType)
tmp = DefaultOverrideDocType;
SetFeatureControl(FeatureBrowserEmulation, (int)tmp);
}
}
#endregion
#region Internet Feature Controls (G)
/// <summary>
/// Internet Explorer 9. Enables Internet Explorer to use a graphics processing unit (GPU) to render content. This dramatically improves performance for webpages that are rich in graphics.
/// By default, this feature is enabled for Internet Explorer and disabled for applications hosting the WebBrowser Control.To enable this feature by using the registry, add the name of your executable file to the following setting.
/// Note: GPU rendering relies heavily on the quality of your video drivers. If you encounter problems running Internet Explorer with GPU rendering enabled, you should verify that your video drivers are up to date and that they support hardware accelerated graphics.
/// </summary>
internal bool GpuRendering
{
get
{
if (_version.Major < 9)
throw new NotSupportedException($"{nameof(GpuRendering)} requires Internet Explorer 9 and Later.");
return Convert.ToBoolean(GetFeatureControl(FeatureGpuRendering));
}
set
{
if (_version.Major < 9)
throw new NotSupportedException($"{nameof(GpuRendering)} requires Internet Explorer 9 and Later.");
SetFeatureControl(FeatureGpuRendering, Convert.ToInt16(value));
}
}
#endregion
#region Internet Feature Controls (L)
/// <summary>
/// Internet Explorer 7 and later. When enabled, feature allows scripts stored in the Local Machine zone to be run only in webpages loaded from the Local Machine zone or by webpages hosted by sites in the Trusted Sites list. For more information, see Security and Compatibility in Internet Explorer 7.
/// By default, this feature is enabled for Internet Explorer and disabled for applications hosting the WebBrowser Control.To enable this feature by using the registry, add the name of your executable file to the following setting.
/// </summary>
internal bool LocalScriptBlocking
{
get
{
if (_version.Major < 7)
throw new NotSupportedException($"{nameof(LocalScriptBlocking)} requires Internet Explorer 7 and Later.");
return Convert.ToBoolean(GetFeatureControl(FeatureBlockLmzScript));
}
set
{
if (_version.Major < 7)
throw new NotSupportedException($"{nameof(LocalScriptBlocking)} requires Internet Explorer 7 and Later.");
SetFeatureControl(FeatureBlockLmzScript, Convert.ToInt16(value));
}
}
#endregion
private DocumentMode DefaultRespectDocType
{
get
{
if (_version.Major >= 11)
return DocumentMode.InternetExplorer11RespectDocType;
switch (_version.Major)
{
case 10:
return DocumentMode.InternetExplorer10RespectDocType;
case 9:
return DocumentMode.InternetExplorer9RespectDocType;
case 8:
return DocumentMode.InternetExplorer8RespectDocType;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private DocumentMode DefaultOverrideDocType
{
get
{
if (_version.Major >= 11)
return DocumentMode.InternetExplorer11OverrideDocType;
switch (_version.Major)
{
case 10:
return DocumentMode.InternetExplorer10OverrideDocType;
case 9:
return DocumentMode.InternetExplorer9OverrideDocType;
case 8:
return DocumentMode.InternetExplorer8OverrideDocType;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
internal enum DocumentMode
{
NotSet = -1,
[Description("Webpages containing standards-based !DOCTYPE directives are displayed in IE latest installed version mode.")]
DefaultRespectDocType,
[Description("Webpages are displayed in IE latest installed version mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive could causes the page to load in Quirks.")]
DefaultOverrideDocType,
[Description(
"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."
)] InternetExplorer11OverrideDocType = 11001,
[Description(
"IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11."
)] InternetExplorer11RespectDocType = 11000,
[Description(
"Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive."
)] InternetExplorer10OverrideDocType = 10001,
[Description(
"Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10."
)] InternetExplorer10RespectDocType = 10000,
[Description(
"Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks."
)] InternetExplorer9OverrideDocType = 9999,
[Description(
"Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.\r\n" +
"Important In Internet Explorer 10, Webpages containing standards - based !DOCTYPE directives are displayed in IE10 Standards mode."
)] InternetExplorer9RespectDocType = 9000,
[Description(
"Webpages are displayed in IE8 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks."
)] InternetExplorer8OverrideDocType = 8888,
[Description(
"Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8\r\n" +
"Important In Internet Explorer 10, Webpages containing standards - based !DOCTYPE directives are displayed in IE10 Standards mode."
)] InternetExplorer8RespectDocType = 8000,
[Description(
"Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control."
)] InternetExplorer7RespectDocType = 7000
}