C# 獲取系統當前IE版本號

唐宋元明清2188發表於2018-11-21

1. 登錄檔中,IE的位置:

計算機HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftInternet Explorer

2. 通過登錄檔讀取IE配置

1     RegistryKey mainKey = Registry.LocalMachine;
2     RegistryKey subKey = mainKey.OpenSubKey(@"SOFTWAREMicrosoftInternet Explorer");

3. 讀取IE的版本號

 1         /// <summary>
 2         /// 獲取IE詳細版本號
 3         /// </summary>
 4         /// <param name="text"></param>
 5         /// <returns></returns>
 6         public static string GetDetailVersion(string text)
 7         {
 8             //通過WebBrowser方案獲取版本號
 9             //int mainVer = (new WebBrowser()).Version.Major;
10 
11             //通過登錄檔獲取使用者IE版本號
12             RegistryKey mainKey = Registry.LocalMachine;
13             RegistryKey subKey = mainKey.OpenSubKey(text);
14 
15             var versionNumber = subKey?.GetValue("svcVersion")?.ToString() ?? string.Empty;
16             if ( string.IsNullOrEmpty(versionNumber))
17             {
18                 versionNumber = subKey?.GetValue("svcUpdateVersion")?.ToString() ?? string.Empty;
19                 if (string.IsNullOrEmpty(versionNumber))
20                 {
21                     versionNumber = subKey?.GetValue("Version")?.ToString() ?? string.Empty;
22                 }
23             }
24             return versionNumber;
25         }

4. 獲取主版本號(8/9/10 etc.)

 1     /// <summary>
 2     /// IE瀏覽器版本號幫助類
 3     /// </summary>
 4     public static class IEVersionHelper
 5     {
 6         /// <summary>
 7         /// 獲取IE主版本號
 8         /// </summary>
 9         /// <param name="text"></param>
10         /// <returns></returns>
11         public static string GetMajorVersion(string text)
12         {
13             var majorVersion = string.Empty;
14 
15             var detailVersion = GetDetailVersion(text);
16             if (!string.IsNullOrWhiteSpace(detailVersion))
17             {
18                 if (detailVersion.IndexOf(".", StringComparison.Ordinal) is int connectedCharFirstIndex && connectedCharFirstIndex > -1)
19                 {
20                     majorVersion = detailVersion.Substring(0, connectedCharFirstIndex);
21                 }
22                 else
23                 {
24                     majorVersion = detailVersion;
25                 }
26             }
27 
28             return majorVersion;
29         }
30 
31         /// <summary>
32         /// 獲取IE詳細版本號
33         /// </summary>
34         /// <param name="text"></param>
35         /// <returns></returns>
36         public static string GetDetailVersion(string text)
37         {
38             //通過登錄檔獲取使用者IE版本號
39             RegistryKey mainKey = Registry.LocalMachine;
40             RegistryKey subKey = mainKey.OpenSubKey(text);
41 
42             var versionNumber = subKey?.GetValue("svcVersion")?.ToString() ?? string.Empty;
43             if ( string.IsNullOrEmpty(versionNumber))
44             {
45                 versionNumber = subKey?.GetValue("svcUpdateVersion")?.ToString() ?? string.Empty;
46                 if (string.IsNullOrEmpty(versionNumber))
47                 {
48                     versionNumber = subKey?.GetValue("Version")?.ToString() ?? string.Empty;
49                 }
50             }
51             return versionNumber;
52         }
53     }

 

相關文章