最近在处理客户端安装程序过程,有一个需求:需要检测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. ArrayList与LinkedList比较

    ArrayList与LinkedList比较 1.实现方式 ArrayList内部结构为数组,定义如下: /** * The array buffer into which the elements ...

  2. Vertx使用EventBus发送接受自定义对象

    先看官方文档步骤: 需要一个编解码器,看源码: 可见内置了需要数据类型的实现,所以发送其他消息可以发送,但是如果发送自定义对象就需要自己实现编解码逻辑了 一 自定义编解码器 /** * 自定义对象编解 ...

  3. 在Centos7上安装Oracle

    环境: 硬盘30G:2G RAM:Centos7:Oracle 11G: 1.创建组和用户 [zzd@localhost ~]$ su root #切换到root Password: [root@lo ...

  4. python sqlalchemy mysql 自动映射

    SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作 简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果 ...

  5. 18、DHCP

    Dynamic Host Configuration Protocol DHCP的前身:Bootstrap DHCP的封装 DHCP基本知识点 1 .DHCP协议在RFC2131中定义,使用udp协议 ...

  6. 如何查看dll或者exe是X86还是X64架构

    使用VS里面的dumpbin.exe 用法:dumpbin /headers *.exe(需要运行vcvarsall.bat) C32 or Winhex PE  L为x86.PE  d†为x64 P ...

  7. WeChall_ Training: Stegano I (Training, Stegano)

    This is the most basic image stegano I can think of. 解题: 一张小图片,文本方式打开.

  8. HDU 6521 K-th Closest Distance (主席树+二分)

    题意: 给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线 思路: 主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid ask该权值线段树里[p-m ...

  9. 一口气说出Redis 5种数据结构及对应使用场景,面试要加分的

    整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 更多优选 一口气说出 9种 分布式ID生成方式,面试官有点懵了 ...

  10. PHP第三方登录——QQ登录

    主要内容 简单回顾OAuth协议基本原理 接入QQ登录的前置条件以及开放平台账号申请 引入官方SDK SDK参数配置 SDK核心方法解读 整合QQ登录SDK到Web应用中 SDK优化 调用API的开发 ...