在Web浏览器控件中使用最新版本的Internet Explorer

Mos*_*026 79 c# webbrowser-control

C#Windows窗体 应用程序中的Web浏览器控件的默认版本是7.我已经通过文章浏览器仿真更改为9 ,但是如何在Web浏览器控件中使用已安装的Internet Explorer的最新版本?

Mhm*_*hmd 89

我看到了Veer的回答.我认为这是对的,但我没有为我工作.也许我正在使用.NET 4并使用64x操作系统,请仔细检查.

您可以在启动应用程序时进行设置或检查:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以找到messagebox.show,仅用于测试.

键如下:

  • 11001(0x2AF9) - Internet Explorer 11.无论!DOCTYPE指令如何,网页都以IE11边缘模式显示.

  • 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) - 无论!DOCTYPE指令如何,网页都以IE8标准模式显示.

  • 8000(0x1F40) - 包含基于标准的!DOCTYPE 指令的网页以IE8模式显示.

  • 7000(0x1B58) - 包含基于标准的!DOCTYPE 指令的网页以IE7标准模式显示.

参考:MSDN:Internet功能控件

我看到像Skype这样的应用程序使用10001.我不知道.

注意

安装应用程序将更改注册表.您可能需要在清单文件中添加一行以避免由于注册表更改权限而导致的错误:

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)

更新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 


}
Run Code Online (Sandbox Code Playgroud)

使用类如下

WebBrowserHelper.FixBrowserVersion();
WebBrowserHelper.FixBrowserVersion("SomeAppName");
WebBrowserHelper.FixBrowserVersion("SomeAppName",intIeVer);
Run Code Online (Sandbox Code Playgroud)

您可能在Windows 10的可比性方面遇到问题,可能由于您的网站本身,您可能需要添加此元标记

<meta http-equiv="X-UA-Compatible" content="IE=11" >
Run Code Online (Sandbox Code Playgroud)

请享用 :)

  • 我知道这已经有几年了,但对于未来的读者:您的应用程序不需要检查它是在 64 位系统中运行还是在 64 位进程中运行。64 位版本的 Windows 实现了 [Registry Redirector](https://docs.microsoft.com/en-us/windows/desktop/winprog64/registry-redirector),它将自动重定向在 64- 上运行的 32 位应用程序位系统到 `Wow6432Node` 子项。您的应用程序不需要做任何额外的事情来适应这个“新”键。 (4认同)
  • 一个建议:将`Environment.Is64BitOperatingSystem`改为`Environment.Is64BitProcess`. (3认同)
  • 来自 MSDN:“注册表重定向器通过在 WOW64 上提供注册表某些部分的单独逻辑视图来隔离 32 位和 64 位应用程序。注册表重定向器拦截 32 位和 64 位注册表调用它们各自的逻辑注册表视图和将它们映射到相应的物理注册表位置。重定向过程对应用程序是透明的。因此,32 位应用程序可以像在 32 位 Windows 上运行一样访问注册表数据,即使数据存储在不同的位置64位Windows”不用担心Wow6432Node (2认同)
  • 您也可以使用 HKEY_CURRENT_USER,无需管理员。 (2认同)

Roo*_*lie 54

使用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);
Run Code Online (Sandbox Code Playgroud)


dov*_*vid 14

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);
Run Code Online (Sandbox Code Playgroud)

根据我在这里阅读(控制WebBrowser控件兼容性:

如果我将FEATURE_BROWSER_EMULATION文档模式值设置为高于客户端上的IE版本会发生什么?

显然,浏览器控件只能支持小于或等于客户端上安装的IE版本的文档模式.使用FEATURE_BROWSER_EMULATION键最适用于具有已部署和支持版本的浏览器的企业级业务应用程序.如果您将值设置为比客户端上安装的浏览器版本更高的浏览器模式,则浏览器控件将选择可用的最高文档模式.

最简单的事就是加一个非常高的十进制数......


Vee*_*eer 12

你可以试试这个链接

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是`Processes.getCurrentProcessName()`?可能是`Process.GetCurrentProcess().ProcessName`? (5认同)
  • 您也可以使用HKEY_CURRENT_USER,无需管理员. (2认同)

use*_*511 11

我没有更改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>
Run Code Online (Sandbox Code Playgroud)

请参阅Web浏览器控件和指定IE版本.