The Webbrowser control in the
C# Windows Forms application uses Internet Explorer 7 by default. Although it can be modified to 9 through the "browser simulation" technology, this cannot access the latest Internet Explorer version.
To enable the Webbrowser control to use the latest Internet Explorer version, you need to set up a registry item. The following is a code fragment:
The registry value of different IE versions
<code class="language-csharp">private void SetIEKeyForWebBrowserControl(string appName, int ieVersion) { try { string keyPath = Environment.Is64BitOperatingSystem ? @"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION" : @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"; Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(keyPath, true); string appKeyName = string.Format("{0}.exe", appName); if (registryKey.GetValue(appKeyName) == null) { registryKey.SetValue(appKeyName, ieVersion, Microsoft.Win32.RegistryValueKind.DWord); MessageBox.Show("应用程序设置已成功应用"); } else { MessageBox.Show("应用程序设置已存在"); } } catch (Exception ex) { MessageBox.Show("应用程序设置失败", ex.Message); } }</code>
The following key values correspond to the specific Internet Explorer version:
11001 (0x2af9) -Tact
11000 (0x2af8) -TactAutomatically set the registry item for the latest IE version
The following is an example of code, which is used to automatically determine and set the registry item for the latest IE version:
<code class="language-csharp">using mshtml; public static int GetLatestIEVersion() { int version = 0; try { object versionObject = InternetExplorerManager.GetActiveVersion(); if (versionObject == null) throw new Exception("无法确定最新的IE版本。"); version = (int)versionObject; } catch { version = 7; } return version; }</code>
Other tips
For 64 -bit operating systems, make sure that the necessary permissions have been granted in the registry, for example, by using the "RequestedExecutionLevel" property by using the "RequestedExecutionLeve" in the application list.
<code class="language-csharp">private void Form1_Load(object sender, EventArgs e) { int latestVersion = GetLatestIEVersion(); SetIEKeyForWebBrowserControl("MyApplication", latestVersion); }</code>
meta marks to your webpage.
This Revised Response Simplifies the Code, Removes The UnnecessaryThe above is the detailed content of How Can I Use the Latest Internet Explorer Version with the C# WebBrowser Control?. For more information, please follow other related articles on the PHP Chinese website!