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

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

编辑1

我特别感兴趣的是确定是否安装了IE9。 可以安装多个IE版本,但特别是IE9导致我的应用程序出现问题。

怎么样;

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

在winform应用程序中还有另一个未提及的问题。 即使安装了IE9,webbrowser也始终使用IE7.0引擎运行。

如果您希望您的应用程序受益于更新的html渲染器,您必须在注册表中编写。 下面的代码适合我。 那是:

  • 进出Visual Studio
  • app用户是否具有管理权限。

     FixBrowserVersion("", 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 } } 

这是如何获得非嵌入式浏览器的版本:

 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("", 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用作root用户。 如果没有管理员权限,请使用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("", 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 } 

您可以从注册表中确定Internet Explorer版本:

 HKLM \ SOFTWARE \ Microsoft \ Internet Explorer \ Version

另请参阅: 确定本地计算机上安装的Internet Explorer版本

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