【问题标题】:How to set IE9 by default for web browser?如何为网络浏览器默认设置 IE9?
【发布时间】:2013-03-30 06:24:49
【问题描述】:

我正在用 C#.NET 开发一个应用程序。我想使用 IE9 版本的 WebBrowser;系统上是否安装了 IE9。

是否有可能将 IE9 与 WebBrower 一起使用,并且可能是 IE9 未安装在我的系统中?

【问题讨论】:

  • 欢迎来到 Stackoverflow。您是否尝试过任何方法来解决此问题?首先展示你的努力。你可以阅读FAQHow to Ask
  • 你想在 IE 而不是默认浏览器中打开链接?或者你想要一个你用来使用 IE 的控件?至于版本,您可能会被他们所拥有的东西所困扰。

标签: c# browser


【解决方案1】:

对于 Windows Internet Explorer 8 或更高版本,FEATURE_BROWSER_EMULATION 功能定义了 Internet Explorer 的默认模拟模式。值 9999 - 强制网页以 IE9 标准模式显示,而不考虑 !DOCTYPE 指令。您需要在目标系统上安装 IE9 或更高版本。检查Internet Feature Controls (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();
    }
}

【讨论】:

  • 将其添加到代码中后,您如何实际使用它?我在 WebBrowserVersionEmulation(?) 中放了什么
  • @pshyoulost 将此函数添加到应用程序加载中
  • 谢谢!我正在用头撞墙,试图让 IE8 正常工作。另一个教程告诉我把这个值放在根本不起作用的 LocalMachine 中。这成功了。
【解决方案2】:

插入

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

进入你的html页面,但是你必须知道Web_browser控件依赖于已经安装在目标操作系统上的IE版本

【讨论】:

    【解决方案3】:

    为此,您必须查找

    • 底层浏览器的版本是什么(注册表键可能返回以前安装的版本)。我使用的最简单的代码是询问 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();
      

    • 在 64bit + 32bit 模式下,可能你也必须在“Software\Wow6432Node”中创建一个条目

    设置注册表项后,WebBrowser 控件应以所需的仿真开始

    【讨论】:

      【解决方案4】:

      不,webbrowser-element(我想你的意思是这个)是基于 IE6 的。您只能从程序中启动 IE9 的进程(不知道名称,firefox 的简称为“firefox.exe”)。

      【讨论】:

        【解决方案5】:

        您可以从注册表中读取版本:

        var ieVersion = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Internet Explorer").GetValue("Version");
        

        如果你有一个 WebBrowser 控件,你可以从那里获取它:

        WebBrowser  browser = new WebBrowser();
        Version ver = browser.Version;
        

        【讨论】:

        • 您好 Gencebay,感谢您的回复。实际上该版本显示 9.0(当我获取及以上代码时)但呈现 HTML 与 IE9 不同。它不仅仅是像 IE8。无论如何我可以设置 WebBrowser 的渲染版本吗?
        • 您可以尝试在 HTML 页面的 标记中添加元标记 。此元标记必须添加到 CSS、JavaScript 文件等的任何链接之前,这些链接也在您的 中才能正常工作(只有其他 标记或 标记可以在它之前)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-12
        相关资源
        最近更新 更多