在C# Windows Forms应用程序中,WebBrowser控件的默认版本是7。我已经通过文章浏览器仿真将其更改为9,但如何在WebBrowser控件中使用安装的Internet Explorer的最新版本呢?
在C# Windows Forms应用程序中,WebBrowser控件的默认版本是7。我已经通过文章浏览器仿真将其更改为9,但如何在WebBrowser控件中使用安装的Internet Explorer的最新版本呢?
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();
}
}
您可能会发现messagebox.show仅供测试使用。
键如下所示:
11001 (0x2AF9) - Internet Explorer 11。页面以IE11边缘模式显示,不考虑
!DOCTYPE
指令。11000 (0x2AF8) - Internet Explorer 11。包含基于标准的
!DOCTYPE
指令的网页以IE11边缘模式显示。IE11的默认值。10001 (0x2711)- Internet Explorer 10。不考虑
!DOCTYPE
指令,页面以IE10标准模式显示。10000 (0x2710)- Internet Explorer 10。包含基于标准的
!DOCTYPE
指令的网页以IE10标准模式显示。Internet Explorer 10的默认值。9999 (0x270F) - Internet Explorer 9。不考虑
!DOCTYPE
指令,页面以IE9标准模式显示。9000 (0x2328) - Internet Explorer 9。包含基于标准的
!DOCTYPE
指令的网页以IE9模式显示。8888 (0x22B8) - 网页以IE8标准模式显示,不考虑
!DOCTYPE
指令。8000 (0x1F40) - 包含基于标准的
!DOCTYPE
指令的网页以IE8模式显示。7000 (0x1B58) - 包含基于标准的
!DOCTYPE
指令的网页以IE7标准模式显示。
参考资料: MSDN:网页功能控件
我看到像Skype这样的应用程序使用10001,但我不知道为什么。
注意
安装程序将更改注册表。您可能需要在清单文件中添加一行以避免由于更改注册表权限而导致的错误:
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
更新 1
这是一个类,将获取 Windows 上的最新版本 IE,并进行应有的更改;
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
}
WebBrowserHelper.FixBrowserVersion();
WebBrowserHelper.FixBrowserVersion("SomeAppName");
WebBrowserHelper.FixBrowserVersion("SomeAppName",intIeVer);
您可能会遇到Windows 10不兼容的问题,这可能是由于您的网站本身导致的。您可能需要添加此元标记。
<meta http-equiv="X-UA-Compatible" content="IE=11" >
享受 :)
使用来自MSDN的数值:
int BrowserVer, RegVal;
// get the installed IE version
using (WebBrowser Wb = new WebBrowser())
BrowserVer = Wb.Version.Major;
// set the appropriate IE version
if (BrowserVer >= 11)
RegVal = 11001;
else if (BrowserVer == 10)
RegVal = 10001;
else if (BrowserVer == 9)
RegVal = 9999;
else if (BrowserVer == 8)
RegVal = 8888;
else
RegVal = 7000;
// set the actual key
using (RegistryKey Key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", RegistryKeyPermissionCheck.ReadWriteSubTree))
if (Key.GetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe") == null)
Key.SetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe", RegVal, RegistryValueKind.DWord);
CreateSubKey
而不是OpenSubKey
,因为如果键不存在,OpenSubKey
将返回null。 - eugvar 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);
根据我在这里阅读的内容 (控制WebBrowser控件兼容性):
如果我将FEATURE_BROWSER_EMULATION文档模式值设置为客户端IE版本更高会发生什么?
显然,浏览器控件只能支持小于或等于客户端安装的IE版本的文档模式。使用FEATURE_BROWSER_EMULATION键最适合企业线上业务应用程序,其中浏览器已部署并得到支持。 如果您将该值设置为一个高于客户端安装的浏览器版本的浏览器模式,则浏览器控件将选择可用的最高文档模式。
最简单的方法是输入非常大的十进制数...
SOFTWARE\WOW6432Node\Microsoft...
下。它在代码中自动重定向,但如果您打开了regedit,则可能会让您感到惊讶。 - toster-cxRegistry.LocalMachine.OpenSubKey("..
是有效的。 - bendecko我不需要更改RegKey,我可以在HTML的头部插入一行代码:
<html>
<head>
<!-- Use lastest version of Internet Explorer -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- Insert other header tags here -->
</head>
...
</html>
请查看Web浏览器控件和指定IE版本。
try
{
var IEVAlue = 9000; // can be: 9999 , 9000, 8888, 8000, 7000
var targetApplication = Processes.getCurrentProcessName() + ".exe";
var localMachine = Registry.LocalMachine;
var parentKeyLocation = @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl";
var keyName = "FEATURE_BROWSER_EMULATION";
"opening up Key: {0} at {1}".info(keyName, parentKeyLocation);
var subKey = localMachine.getOrCreateSubKey(parentKeyLocation,keyName,true);
subKey.SetValue(targetApplication, IEVAlue,RegistryValueKind.DWord);
return "all done, now try it on a new process".info();
}
catch(Exception ex)
{
ex.log();
"NOTE: you need to run this under no UAC".info();
}
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
,但这种方法更简单。谢谢。 - Moslem7026Processes.getCurrentProcessName()
是什么?可能是 Process.GetCurrentProcess().ProcessName
吗? - SerG我能够实现Luca的解决方案,但是我需要做一些修改才能让它正常工作。我的目标是将D3.js与Windows Forms应用程序的Web浏览器控件结合使用(针对.NET 2.0)。现在它对我来说可以工作了。希望这能对其他人有所帮助。
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();
}
}
}
}
此外,我在项目设置中添加了一个字符串(ie_emulation),其值为11999。这个值似乎可以用于IE11(11.0.15)。
接下来,我需要更改应用程序的权限以允许访问注册表。这可以通过向您的项目添加新项(使用VS2012)来完成。在常规项目下,选择应用程序清单文件。将级别从asInvoker更改为requireAdministrator(如下所示)。
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
如果您正在尝试使用webbrowser控件与D3.js一起使用,您可能需要修改JSON数据并将其存储在HTML页面内的变量中,因为D3.json使用XmlHttpRequest(更容易与web服务器一起使用)。经过这些更改和上述更改后,我的Windows表单能够加载调用D3的本地HTML文件。
这里是我通常使用的方法,适用于32位和64位应用程序;ie_emulation可以是任何在此处记录的文档: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();
}
}
结合RooiWillie和MohD的答案,记得以管理员权限运行您的应用程序。
var appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
RegistryKey Regkey = null;
try
{
int BrowserVer, RegVal;
// get the installed IE version
using (WebBrowser Wb = new WebBrowser())
BrowserVer = Wb.Version.Major;
// set the appropriate IE version
if (BrowserVer >= 11)
RegVal = 11001;
else if (BrowserVer == 10)
RegVal = 10001;
else if (BrowserVer == 9)
RegVal = 9999;
else if (BrowserVer == 8)
RegVal = 8888;
else
RegVal = 7000;
//For 64 bit Machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit Machine
Regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
//If the path is not correct or
//If user't have priviledges to access registry
if (Regkey == null)
{
MessageBox.Show("Registry Key for setting IE WebBrowser Rendering Address Not found. Try run the program with administrator's right.");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
//Check if key is already present
if (FindAppkey == RegVal.ToString())
{
Regkey.Close();
return;
}
Regkey.SetValue(appName, RegVal, RegistryValueKind.DWord);
}
catch (Exception ex)
{
MessageBox.Show("Registry Key for setting IE WebBrowser Rendering failed to setup");
MessageBox.Show(ex.Message);
}
finally
{
//Close the Registry
if (Regkey != null)
Regkey.Close();
}
<meta http-equiv="X-UA-Compatible" content="IE=11" >
Environment.Is64BitOperatingSystem
为Environment.Is64BitProcess
。 - CC IncWow6432Node
子键。您的应用程序不需要做任何额外的工作来适应这个“新”的键。 - Visual Vincent