最近在处理客户端安装程序过程, 有一个需求: 需要检测 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 安装信息:
- /// <summary>
- /// 检测本地是否安装 wps
- /// </summary>
- /// <returns></returns>
- public string CheckWpsExsitStatus()
- {
- string wpsJudge = string.Empty;
- try
- {
- // 获取 Windows 注册表基项 HKEY_LOCAL_MACHINE.
- RegistryKey registryKey = Registry.LocalMachine;
- // 确定当前操作系统是否为 64 位操作系统 (支持. NetFrame Work 4.0+)
- if (Environment.Is64BitOperatingSystem)
- // 64 位操作系统
- registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
- else
- // 32 位操作系统
- registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
- // 读取注册表信息 32
- RegistryKey wpsKey1 = registryKey.OpenSubKey(@"SOFTWARE\Kingsoft\Office\6.0\common");
- if (wpsKey1 != null)
- {
- string filewps = wpsKey1.GetValue("InstallRoot").ToString();
- if (File.Exists(filewps + @"\office6\et.exe"))
- {
- wpsJudge = "本电脑安装了 Wps+Path=" + @"SOFTWARE\Kingsoft\Office\6.0\common";
- }
- }
- // 读取注册表信息 6432
- RegistryKey wpsKey2 = registryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\Kingsoft\Office\6.0\common");
- if (wpsKey1 != null)
- {
- string filewps = wpsKey2.GetValue("InstallRoot").ToString();
- if (File.Exists(filewps + @"\office6\et.exe"))
- {
- wpsJudge = "本电脑安装了 Wps+Path=" + @"SOFTWARE\Wow6432Node\Kingsoft\Office\6.0\common";
- }
- }
- if (wpsJudge == string.Empty)
- wpsJudge = "未安装 wps!";
- }
- catch (Exception ex)
- {
- wpsJudge = "检测失败!" + ex.Message;
- }
- return wpsJudge;
- }
检测 Office 安装情况:
- /// <summary>
- /// 检测本地是否安装 Office
- /// </summary>
- /// <param name="officeVersion">Office 版本代号: 14.0(office2010)</param>
- /// <returns></returns>
- public string CheckOfficeExsitStatus(string officeVersion)
- {
- string officeJudge = string.Empty;
- string officeVersionInfo = string.Empty;
- if (string.IsNullOrEmpty(officeVersion))
- return officeJudge;
- try
- {
- // 是否安装 Office
- bool IsInstall = false;
- // 系统版本
- bool IsSys64Bit = true;
- //Office 安装位数 1=32(NoWow6432Node);2=64(Wow6432Node)
- int IofficeSetInfo = 0;
- // 获取 Windows 注册表基项 HKEY_LOCAL_MACHINE.
- RegistryKey registryKey = Registry.LocalMachine;
- // 确定当前操作系统是否为 64 位操作系统 (支持. NetFrame Work 4.0+;4.0 以下 可以去除当前判断部分)
- if (Environment.Is64BitOperatingSystem)
- // 64 位操作系统
- registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
- else
- // 32 位操作系统
- IsSys64Bit = false; registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
- // 32 位操作系统?
- RegistryKey officeKey1 = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\" + officeVersion + @"\Word\InstallRoot");
- if (officeKey1 != null)
- {
- if (officeKey1.GetValue("Path") != null)
- {
- string filewps = officeKey1.GetValue("Path").ToString();
- if (File.Exists(filewps + "WINWORD.exe"))
- {
- IofficeSetInfo = 1;
- IsInstall = true;
- }
- }
- }
- //64 位操作系统安装 32 位软件 ?
- RegistryKey officeKey2 = registryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Office\" + officeVersion + @"\Word\InstallRoot");
- if (officeKey2 != null)
- {
- if (officeKey2.GetValue("Path") != null)
- {
- string filewps = officeKey2.GetValue("Path").ToString();
- if (File.Exists(filewps + "WINWORD.exe"))
- {
- IofficeSetInfo = 2;
- IsInstall = true;
- }
- }
- }
- // 已经安装
- if (IsInstall)
- {
- //64 位操作系统
- if (IsSys64Bit)
- {
- // 使用 Office 位数信息
- if (IofficeSetInfo == 1)
- {
- officeVersionInfo = "当前安装 office 版本为 64 位";
- }
- else if (IofficeSetInfo == 2)
- {
- officeVersionInfo = "当前安装 office 版本为 32 位";
- }
- }
- else
- {
- if (IofficeSetInfo == 1)
- {
- officeVersionInfo = "当前安装 office 版本为 32 位";
- }
- else if (IofficeSetInfo == 2)
- {
- officeVersionInfo = "当前安装 office 版本为 64 位";
- }
- }
- officeVersionInfo = officeVersionInfo + $"IsSys64Bit={IsSys64Bit},IofficeSetInfo={IofficeSetInfo}";
- }
- }
- catch (Exception ex)
- {
- officeVersionInfo = "检测失败!" + ex.Message;
- }
- return officeVersionInfo;
- }
获取 Office 版本名称
- /// <summary>
- /// 返回 Office 版本 (暂不包含 office2019 )
- /// </summary>
- /// <param name="versionNum">Office 版本代号 </param>
- /// <returns></returns>
- public string GetOfficeVersionName(string versionNum)
- {
- string strDesc = string.Empty;
- switch (versionNum)
- {
- case "8.0": { strDesc = "office97"; } break;
- case "9.0": { strDesc = "office2000"; } break;
- case "10.0": { strDesc = "officexp(2002)"; } break;
- case "11.0": { strDesc = "office2003"; } break;
- case "12.0": { strDesc = "office2007"; } break;
- case "14.0": { strDesc = "office2010"; } break;
- case "15.0": { strDesc = "office2013"; } break;
- case "16.0": { strDesc = "office2016"; } break;
- default: strDesc = "未找到匹配内容: version=" + versionNum; break;
- }
- return strDesc;
- }
测试代码:
- /// <summary>
- /// 获取 Office 安装情况
- /// </summary>
- public void GetVersionIsInstall()
- {
- var strArray = new string[] { "8.0", "9.0", "10.0", "11.0", "12.0", "13.0", "14.0", "15.0", "16.0" };
- foreach (var item in strArray)
- {
- var setInfo = CheckOfficeExsitStatus(item);// 获取安装 Office 情况信息
- if (!string.IsNullOrEmpty(setInfo))
- Console.WriteLine("系统安装 Office 版本为:" + GetOfficeVersionName(item));
- Console.WriteLine("item=" + item + ";" + setInfo);
- }
- }
测试效果截图:
以上在 Windows 7 以及 Windows Server 2008 R2 系统测试, 可以使用; 如有不合理之处, 请大家多多指教.
来源: http://www.bubuko.com/infodetail-3379816.html