打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
获取Windows平台下 安装office 版本位数信息

  最近在处理客户端安装程序过程,有一个需求:需要检测Windows平台下安装office 版本信息以及获取使用的office是32 位还是64 位; 当检测出office 位数为64位时,提示当前office 不支持程序的使用。

  找了很多资料,一般情况下,是不能直接获取office 安装位数信息的;加上Windows 32 位与64位系统 ,安装使用的office在不同Windows系统下注册表位置不一样,久久不能解决这个需求。

  话不多说,先记录一下代码。

注意事项:

  Environment.Is64BitOperatingSystem   ......//判断当前windows是否为64位操作系统   // 支持 .NetFrame Work 4.0+

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

          //确定当前操作系统是否为 64 位操作系统                if (Environment.Is64BitOperatingSystem)                    // 64 位操作系统                    registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);                else                    // 32 位操作系统                    registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);

 

 

 

检测注册表是否有wps安装信息:

 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                 else17                     // 32 位操作系统18                     registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);19 20                 //读取注册表信息 3221                 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                 //读取注册表信息 643231                 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 安装情况:

 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             try13             {14                 //是否安装office15                 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                 else28                     // 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                     else78                     {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 版本名称

 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         }

     测试代码:

 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 系统测试,可以使用;      如有不合理之处,请大家多多指教。

如果您觉得本文对您有帮助,欢迎点击“推荐”按钮,您的“推荐”将是我最大的写作动力!(/:微笑)欢迎转载,转载请注明出处。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#设置开机启动
MySQL数据库介绍
利用JAVA向注册表里写信息
C#操作注册表全攻略
C#操作注册表
C#读、写、删除注册表
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服