獲取Windows平臺下 安裝office 版本位數信息

  最近在處理客戶端安裝程序過程,有一個需求:須要檢測Windows平臺下安裝office 版本信息以及獲取使用的office是32 位仍是64 位; 當檢測出office 位數爲64位時,提示當前office 不支持程序的使用。windows

  找了不少資料,通常狀況下,是不能直接獲取office 安裝位數信息的;加上Windows 32 位與64位系統 ,安裝使用的office在不一樣Windows系統下注冊表位置不同,久久不能解決這個需求。測試

  話很少說,先記錄一下代碼。spa

注意事項:操作系統

  Environment.Is64BitOperatingSystem   ......//判斷當前windows是否爲64位操做系統   // 支持 .NetFrame Work 4.0+code

  RegistryKey.OpenBaseKey       .... // 支持 .NetFrame Work 4.0+blog

          //肯定當前操做系統是否爲 64 位操做系統
                if (Environment.Is64BitOperatingSystem)
                    // 64 位操做系統
                    registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                else
                    // 32 位操做系統
                    registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);

 

 

 

檢測註冊表是否有wps安裝信息:string

 1         /// <summary>
 2         /// 檢測本地是否安裝wps
 3         /// </summary>
 4         /// <returns></returns>
 5         public string CheckWpsExsitStatus()
 6         {
 7             string wpsJudge = string.Empty;
 8             try
 9             {
10                 //獲取 Windows 註冊表基項 HKEY_LOCAL_MACHINE。
11                 RegistryKey registryKey = Registry.LocalMachine;
12                 //肯定當前操做系統是否爲 64 位操做系統(支持.NetFrame Work 4.0+)
13                 if (Environment.Is64BitOperatingSystem)
14                     // 64 位操做系統
15                     registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
16                 else
17                     // 32 位操做系統
18                     registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
19 
20                 //讀取註冊表信息 32
21                 RegistryKey wpsKey1 = registryKey.OpenSubKey(@"SOFTWARE\Kingsoft\Office\6.0\common");
22                 if (wpsKey1 != null)
23                 {
24                     string filewps = wpsKey1.GetValue("InstallRoot").ToString();
25                     if (File.Exists(filewps + @"\office6\et.exe"))
26                     {
27                         wpsJudge = "本電腦安裝了Wps+Path=" + @"SOFTWARE\Kingsoft\Office\6.0\common";
28                     }
29                 }
30                 //讀取註冊表信息 6432
31                 RegistryKey wpsKey2 = registryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\Kingsoft\Office\6.0\common");
32                 if (wpsKey1 != null)
33                 {
34                     string filewps = wpsKey2.GetValue("InstallRoot").ToString();
35                     if (File.Exists(filewps + @"\office6\et.exe"))
36                     {
37                         wpsJudge = "本電腦安裝了Wps+Path=" + @"SOFTWARE\Wow6432Node\Kingsoft\Office\6.0\common";
38                     }
39                 }
40 
41                 if (wpsJudge == string.Empty)
42                     wpsJudge = "未安裝wps!";
43             }
44             catch (Exception ex)
45             {
46                 wpsJudge = "檢測失敗!" + ex.Message;
47             }
48 
49             return wpsJudge;
50         }

 

   檢測office 安裝狀況:it

 1         /// <summary>
 2         /// 檢測本地是否安裝Office
 3         /// </summary>
 4         /// <param name="officeVersion">office 版本代號:14.0(office2010)</param>
 5         /// <returns></returns>
 6         public string CheckOfficeExsitStatus(string officeVersion)
 7         {
 8             string officeJudge = string.Empty;
 9             string officeVersionInfo = string.Empty;
10             if (string.IsNullOrEmpty(officeVersion))
11                 return officeJudge;
12             try
13             {
14                 //是否安裝office
15                 bool IsInstall = false;
16                 //系統版本
17                 bool IsSys64Bit = true;
18                 //office 安裝位數 1=32(NoWow6432Node);2=64(Wow6432Node)
19                 int IofficeSetInfo = 0;
20 
21                 //獲取 Windows 註冊表基項 HKEY_LOCAL_MACHINE。
22                 RegistryKey registryKey = Registry.LocalMachine;
23                 //肯定當前操做系統是否爲 64 位操做系統(支持.NetFrame Work 4.0+;4.0如下 能夠去除當前判斷部分
24                 if (Environment.Is64BitOperatingSystem)
25                     // 64 位操做系統
26                     registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
27                 else
28                     // 32 位操做系統
29                     IsSys64Bit = false; registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
30 
31           // 32位操做系統?
32                 RegistryKey officeKey1 = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\" + officeVersion + @"\Word\InstallRoot");
33                 if (officeKey1 != null)
34                 {
35                     if (officeKey1.GetValue("Path") != null)
36                     {
37                         string filewps = officeKey1.GetValue("Path").ToString();
38                         if (File.Exists(filewps + "WINWORD.exe"))
39                         {
40                             IofficeSetInfo = 1;
41                             IsInstall = true;
43                         }
44                     }
45                 }
46           //64位操做系統安裝32位軟件 ?
47                 RegistryKey officeKey2 = registryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Office\" + officeVersion + @"\Word\InstallRoot");
48                 if (officeKey2 != null)
49                 {
50                     if (officeKey2.GetValue("Path") != null)
51                     {
52                         string filewps = officeKey2.GetValue("Path").ToString();
53                         if (File.Exists(filewps + "WINWORD.exe"))
54                         {
55                             IofficeSetInfo = 2;
56                             IsInstall = true;
58                         }
59                     }
60                 }
61                 //已經安裝
62                 if (IsInstall)
63                 {
64                     //64位操做系統
65                     if (IsSys64Bit)
66                     {
67                         //使用office 位數信息
68                         if (IofficeSetInfo == 1)
69                         {
70                             officeVersionInfo = "當前安裝office 版本爲64位";
71                         }
72                         else if (IofficeSetInfo == 2)
73                         {
74                             officeVersionInfo = "當前安裝office 版本爲32位";
75                         }
76                     }
77                     else
78                     {
79                         if (IofficeSetInfo == 1)
80                         {
81                             officeVersionInfo = "當前安裝office 版本爲32位";
82                         }
83                         else if (IofficeSetInfo == 2)
84                         {
85                             officeVersionInfo = "當前安裝office 版本爲64位";
86                         }
87                     }
88                     officeVersionInfo = officeVersionInfo + $"IsSys64Bit={IsSys64Bit},IofficeSetInfo={IofficeSetInfo}";
89                 }
90 
91             }
92             catch (Exception ex)
93             {
94                 officeVersionInfo = "檢測失敗!" + ex.Message;
95             }
96 
97             return officeVersionInfo;
98         }

獲取office 版本名稱io

 1         /// <summary>
 2         /// 返回office 版本(暫不包含office2019 )
 3         /// </summary>
 4         /// <param name="versionNum">office 版本代號</param>
 5         /// <returns></returns>
 6         public string GetOfficeVersionName(string versionNum)
 7         {
 8             string strDesc = string.Empty;
 9             switch (versionNum)
10             {
11                 case "8.0": { strDesc = "office97"; } break;
12                 case "9.0": { strDesc = "office2000"; } break;
13                 case "10.0": { strDesc = "officexp(2002)"; } break;
14                 case "11.0": { strDesc = "office2003"; } break;
15                 case "12.0": { strDesc = "office2007"; } break;
16                 case "14.0": { strDesc = "office2010"; } break;
17                 case "15.0": { strDesc = "office2013"; } break;
18                 case "16.0": { strDesc = "office2016"; } break;
19                 default: strDesc = "未找到匹配內容:version=" + versionNum; break;
20             }
21 
22             return strDesc;
23         }

     測試代碼:class

 1  /// <summary>
 2         /// 獲取office 安裝狀況
 3         /// </summary>
 4         public void GetVersionIsInstall()
 5         {
 6             var strArray = new string[] { "8.0", "9.0", "10.0", "11.0", "12.0", "13.0", "14.0", "15.0", "16.0" };
 7             foreach (var item in strArray)
 8             {
 9                 var setInfo = CheckOfficeExsitStatus(item);//獲取安裝office 狀況信息
10                 if (!string.IsNullOrEmpty(setInfo))
11                     Console.WriteLine("系統安裝Office版本爲:" + GetOfficeVersionName(item));
12                 Console.WriteLine("item=" + item + ";" + setInfo);
13             }
14         }

測試效果截圖:

 

 

以上在Windows 7 以及 Windows Server 2008 R2 系統測試,可使用;      若有不合理之處,請你們多多指教。

若是您以爲本文對您有幫助,歡迎點擊「推薦」按鈕,您的「推薦」將是我最大的寫做動力!(/:微笑)歡迎轉載,轉載請註明出處。

相關文章
相關標籤/搜索