最近在处理客户端安装程序过程,有一个需求:需要检测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 = ; //获取 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 = ;
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 = ;
IsInstall = true;
}
}
}
//已经安装
if (IsInstall)
{
//64位操作系统
if (IsSys64Bit)
{
//使用office 位数信息
if (IofficeSetInfo == )
{
officeVersionInfo = "当前安装office 版本为64位";
}
else if (IofficeSetInfo == )
{
officeVersionInfo = "当前安装office 版本为32位";
}
}
else
{
if (IofficeSetInfo == )
{
officeVersionInfo = "当前安装office 版本为32位";
}
else if (IofficeSetInfo == )
{
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 系统测试,可以使用;      如有不合理之处,请大家多多指教。

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

获取Windows平台下 安装office 版本位数信息的更多相关文章

  1. Arduino可穿戴开发入门教程Windows平台下安装Arduino IDE

    Arduino可穿戴开发入门教程Windows平台下安装Arduino IDE Windows平台下安装Arduino IDE Windows操作系统下可以使用安装向导和压缩包形式安装.下面详细讲解这 ...

  2. 在Windows平台下安装与配置Memcached及C#使用方法

    1.在Windows下安装Memcached 资料来源:http://www.jb51.net/article/30334.htm 在Windows平台下安装与配置Memcached的方法,Memca ...

  3. Windows 平台下安装Cygwin后,sshd服务无法启动

    Windows 平台下安装Cygwin后,sshd服务无法启动 系统日志记录信息: 事件 ID ( 0 )的描述(在资源( sshd )中)无法找到.本地计算机可能没有必要的注册信息或消息 DLL 文 ...

  4. [转]Windows平台下安装Hadoop

    1.安装JDK1.6或更高版本 官网下载JDK,安装时注意,最好不要安装到带有空格的路径名下,例如:Programe Files,否则在配置Hadoop的配置文件时会找不到JDK(按相关说法,配置文件 ...

  5. MongoDB学习总结(一) —— Windows平台下安装

    > 基本概念 MongoDB是一个基于分布式文件存储的开源数据库系统,皆在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB将数据存储为一个文档,数据结构由键值key=>val ...

  6. 如何在Windows平台下安装配置Memcached

    Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一 ...

  7. 在Windows平台下安装与配置Memcached的方法分享

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  8. Windows平台下安装Eclipse插件,开发Hadoop应用

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  9. windows平台下安装linux

    win10系统下,可直接安装linux系统,且可打开linux的命令行 安装 1.开始--->微软应用商店 进入之后,在搜索框,搜索linux 在搜索结果中,选择要安装的 linux 系统 这里 ...

随机推荐

  1. Oracle 数据库 回滚

    1.打开Flash存储的权限ALTER TABLE authorization ENABLE row movement ;2.把表还原到指定时间点flashback table authorizati ...

  2. Docker快速上手之部署SpringBoot项目

    Docker是基于Go语言实现的云开源项目. Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装.分发.部署.运行等生命周期 ...

  3. QIIME2使用方法

    激活qiime2的执行环境:source activate qiime2-2019.4如何查看conda已有的环境:conda info -e 以下分析流程参考:https://docs.qiime2 ...

  4. DjangoBBS项目功能拆分

    目录 1.随机验证码 2.注册功能 3.登录功能 4.登录认证装饰器配置 5.修改密码模态框方法 6.修改头像 7.修改签名模态框方法 8.注销功能模态框 9.用户上传静态文件配置 10.图片防盗链 ...

  5. 解决dotnet错误 System.InvalidOperationException Message=Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.

    开始=>设置=>manage user certificats  (管理用户证书),里面所有的.net core的全部删除 然后控制台执行: dotnet dev-certs https ...

  6. archlinux+UEFI模式在linux主机下基于KVM-QEMU命令行虚拟机安装笔记

    ArchLinux十分精简,并且具有强大的滚动更新.最近在基于ubuntu的宿主机下通过KVM-QEMU虚拟机安装了archlinux,将过程记录下来以供参考. 1.下载启动盘 1.1.下载archl ...

  7. linux下的 $ ./configure $ sudo make && sudo make install

    $ ./configure $ sudo make && sudo make install 1)./configure命令就是执行当前目录的名为configure的脚本,主要的作用是 ...

  8. ubuntu16.04+Opencv3.4.0安装(slam版)

    本文记录ubuntu下安装opencv过程,步骤来自 opencv官网可以对照官网步骤:https://docs.opencv.org/3.4.0/d7/d9f/tutorial_linux_inst ...

  9. POJ_1376_bfs

    题目描述: 给定一个黑白格子的图,黑格子是障碍物,一个线段交点的起点,一个线段交点的终点和初始方向,机器人从起点开始,只能沿着线段,走到终点,期间不能沿着障碍物边缘和墙边缘. 一次操作可以向当前方向走 ...

  10. HDU_3853_概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3853 又因为总期望为子期望的加权和,加权因子为子期望的转移概率,所以得到:dp[ i ][ j ]= p1 * ...