我正在用 C#.NET 开发一个应用程序。我想使用 IE9 版本的 WebBrowser;IE9 是否安装在系统上。
是否有可能将 IE9 与 WebBrower 一起使用,并且可能是 IE9 未安装在我的系统中?
对于 Windows Internet Explorer 8 或更高版本,FEATURE_BROWSER_EMULATION 功能定义了 Internet Explorer 的默认仿真模式。值 9999 - 强制网页以 IE9 标准模式显示,无论 !DOCTYPE 指令如何。您需要在目标系统上安装 IE9 或更高版本。检查Internet 功能控制 (B..C)
private static void WebBrowserVersionEmulation()
{
const string BROWSER_EMULATION_KEY =
@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
//
// app.exe and app.vshost.exe
String appname = Process.GetCurrentProcess().ProcessName + ".exe";
//
// Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
const int browserEmulationMode = 9999;
RegistryKey browserEmulationKey =
Registry.CurrentUser.OpenSubKey(BROWSER_EMULATION_KEY,RegistryKeyPermissionCheck.ReadWriteSubTree) ??
Registry.CurrentUser.CreateSubKey(BROWSER_EMULATION_KEY);
if (browserEmulationKey != null)
{
browserEmulationKey.SetValue(appname, browserEmulationMode, RegistryValueKind.DWord);
browserEmulationKey.Close();
}
}
插入
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE="\9\" >"
进入您的 html 页面,但您必须知道Web_browser
依赖于已安装在目标操作系统上的 IE 版本的控制
为此,您必须查找
底层浏览器的版本是什么(注册表项可能会返回以前安装的版本)。我使用的最简单的代码是询问 WebBrowser 控件
WebBrowser browser= new WebBrowser
Version ver= browser.Version(); // With ver.Major you can decide the EMULATION
您的应用程序的 app-exe 名称(在 vs 调试环境中运行时与“myapp”.vshost.exe 不同)。我在某处找到的这段代码:
// This code detects the .vshost. when running in vs ide
[DllImport("kernel32.dll", SetLastError=true)]
private static extern int GetModuleFileName([In]IntPtr hModule,
[Out]StringBuilder lpFilename,
[In][MarshalAs(UnmanagedType.U4)] int nSize);
public static String getAppExeName()
{
StringBuilder appname= new StringBuilder(1024);
GetModuleFileName(IntPtr.Zero, appname, appname.Capacity);
return Path.GetFileName(appname.ToString()); // return filename part
}
现在,您可以计算浏览器兼容性所必需的 Registry-Entry。条目可能在 Registry.LocalMachine(需要访问权限)或 Registry.CurrentUser 中。我在每个程序启动时检查注册表,所以,我首先测试条目是否存在
string regSubKey= @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string version= "" + ver.Version + "0000"; // installed version x 10000
string appname= getAppExeName();
RegistryKey rs = Registry.CurrentUser.OpenSubKey(regSubKey);
keyval = rs.GetValue(appname);
rs.Close();
if (keyval != null && keyval.ToString().Equals(version))
return; // already done and no browser update installed.
//
// Create key for this app and this version
rs = Registry.LocalMachine.CreateSubKey(regSubKey);
rs.SetValue(app, sversion, RegistryValueKind.DWord);
rs.Flush();
rs.Close();
在 64 位 + 32 位模式下,您可能还必须在“Software\Wow6432Node”中创建一个条目
设置注册表项后,WebBrowser 控件应以所需的仿真开始
您可以从注册表中读取版本:
var ieVersion = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Internet Explorer").GetValue("Version");
或者
如果你有一个 WebBrowser 控件,你可以从那里获取它:
WebBrowser browser = new WebBrowser();
Version ver = browser.Version;
不,webbrowser 元素(我想你的意思是这个)是基于 IE6 的。您只能从程序中启动 IE9 的进程(不知道名称,但对于 firefox,它只是“firefox.exe”)。