从WinForms应用程序中检测IE版本

9

从WinForms应用程序中检测计算机上安装的IE版本是否可能?

编辑1

我特别想知道是否安装了IE9。可能安装了多个IE版本,但是我的应用程序遇到问题的是IE9。


如果安装了多个IE怎么办? - Dyppl
@Dyppl - 说得有道理,我的情况是特别关心IE9是否已安装。我会更新问题的 :) - Godders
@Godders 如何在同一台机器上安装多个版本的IE? - Ganesh R.
5个回答

18

这怎么样;

string ver = (new WebBrowser()).Version.ToString();

2
+1 我测试过了,它真的很好用。我认为比其他答案容易多了。 - Richard Brightwell
非常简洁明了 - 谢谢!@Richard,也感谢您的测试 :) - Godders

8
在一个Winform应用程序中,还有一个问题没有被提到。即使安装了IE9,WebBrowser仍然使用IE7.0引擎运行。
如果您希望应用程序受益于更近期的HTML渲染器,您需要在注册表中编写代码。下面的代码对我有效。即:
  • in and out of Visual studio
  • whether the app user has administrative rights or not.

    FixBrowserVersion("<YourAppName>", 9000);
    
    private static void FixBrowserVersion(string appName, int lvl)
    {
        FixBrowserVersion2("HKEY_CURRENT_USER", appName+".exe", lvl);
        FixBrowserVersion2("HKEY_LOCAL_MACHINE", appName+".exe", lvl);
        FixBrowserVersion2("HKEY_CURRENT_USER", appName+".vshost.exe", lvl);
        FixBrowserVersion2("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", lvl);
    }
    
    private static void FixBrowserVersion2(string root, string appName, int lvl)
    {
        try
        {
            Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, lvl);
        }
        catch (Exception) 
        {
             // some config will hit access rights exceptions
             // this is why we try with both LOCAL_MACHINE and CURRENT_USER
        }
    }
    

你把顺序搞混了,在本地机器上创建了两次密钥,而且从来没有在current_user中创建过,vshost.exe文件也是一样的。 - Stefan Steiger
确实有一个错误。我想我修复了它(几年后...) - Pascal Ganaye

7
这是获取非嵌入式浏览器版本的方法:
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 

然后您可以设置嵌入式浏览器版本: 32位:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

对于64位:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

作为一个名为"yourapp.exe"的新32位DWORD键。
// FixBrowserVersion("<YourAppName>", 9000);
public static void FixBrowserVersion(string appName, int lvl)
{
    FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", lvl);
    FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", lvl);
    FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", lvl);
    FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", lvl);
}


private static void FixBrowserVersion_Internal(string root, string appName, int lvl)
{
    try
    {

        Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, lvl);
    }
    catch (Exception)
    {
        // some config will hit access rights exceptions
        // this is why we try with both LOCAL_MACHINE and CURRENT_USER
    }
}

你可以使用HKLM和HKCU作为根。如果没有管理员权限,请使用HKCU。
有关更多信息,请参见此处此处
例如:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION]
"MyApp.exe"=dword:0000270f

把所有东西放在一起:
EmbeddedBrowserHelper.FixBrowserVersion();

使用这个类
public class EmbeddedBrowserHelper 
{


    public enum BrowserVersion : int
    {
        IE7 = 7000, // 0x1B58
        IE8 = 8888, // 0x22B8
        IE9 = 9999, // 0x270F
        IE10 = 10001, // 0x2AF7
        IE11 = 11001, // 0x2EDF
        IE12 = 12001,
    } // End Enum BrowserVersion


    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
        {
            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 


}

3

0

查看iexplore.exe的文件版本。如果您担心安装了多个版本,请检查用于HTML文件的文件关联中使用的版本。


谢谢你的回答。你有关于如何从C#代码着手解决这个问题的指导吗? - Godders
@Godders - 我的答案需要比 @AlexK. 或 @dtb 的代码多得多。个人而言,我会先尝试他们的方法。不过我会看看能否提供一些代码示例,以防这些方法不起作用。 - Richard Brightwell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接