[代码]获取系统安装的MS Offfice信息(是否安装、版本、安装路径)

龙宜坡 2009-04-13 06:40:43
N久么发过贴,冒个泡,发个代码,不妥之处请指正!
参考了高手用VB6.0写的

using System;
using System.Collections.Generic;
using System.Text;

namespace WpsTest
{
public class OfficeOperator
{
#region 方法

#region public static
/// <summary>
/// 检测MS-Office是否正确安装
/// 通过注册表检测
/// </summary>
/// <param name="version">获得安装的版本号,如office2000,office2003,office2007</param>
/// <returns></returns>
public static bool IsInstall(out string Version)
{
bool result = false;
string officePath = "";
string officeVersion = "";

Version = "";
GetOfficePath(out officePath, out officeVersion);

if (!string.IsNullOrEmpty(officeVersion) && !string.IsNullOrEmpty(officePath))
{
result = true;
Version = officeVersion;
}

return result;
}

/// <summary>
/// 获取当前某个版本Office的安装路径
/// </summary>
/// <param name="Path">返回当前系统Office安装路径</param>
/// <param name="Version">返回当前系统Office版本信息</param>
public static void GetOfficePath(out string Path,out string Version)
{
string strPathResult = "";
string strVersionResult = "";
string strKeyName = "Path";
object objResult = null;
Microsoft.Win32.RegistryValueKind regValueKind;
Microsoft.Win32.RegistryKey regKey = null;
Microsoft.Win32.RegistryKey regSubKey = null;

try
{
regKey = Microsoft.Win32.Registry.LocalMachine;

if (regSubKey == null)
{//office97
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\8.0\Common\InstallRoot", false);
strVersionResult = "office97";
strKeyName = "OfficeBin";
}

if (regSubKey == null)
{//Office2000
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\9.0\Common\InstallRoot", false);
strVersionResult = "office2000";
strKeyName = "Path";
}

if (regSubKey == null)
{//officeXp
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\10.0\Common\InstallRoot", false);
strVersionResult = "officeXP";
strKeyName = "Path";
}

if (regSubKey == null)
{//Office2003
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot", false);
strVersionResult = "office2003";
strKeyName = "Path";
}

if (regSubKey == null)
{//office2007
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot", false);
strVersionResult = "office2007";
strKeyName = "Path";
}

objResult = regSubKey.GetValue(strKeyName);
regValueKind = regSubKey.GetValueKind(strKeyName);
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
{
strPathResult = objResult.ToString();
}
}
catch (System.Security.SecurityException ex)
{
throw new System.Security.SecurityException("您没有读取注册表的权限", ex);
}
catch (Exception ex)
{
throw new Exception("读取注册表出错!", ex);
}
finally
{

if (regKey != null)
{
regKey.Close();
regKey = null;
}

if (regSubKey != null)
{
regSubKey.Close();
regSubKey = null;
}
}

Path = strPathResult;
Version = strVersionResult;
}
#endregion public static

#endregion 方法
}
}


以下是使用

        private void btnGetOfficePath_Click(object sender, EventArgs e)
{
string officePath = "";
string officeVersion = "";
try
{
OfficeOperator.GetOfficePath(out officePath,out officeVersion);
}
catch (Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
if (!string.IsNullOrEmpty(officePath) && !string.IsNullOrEmpty(officeVersion))
{
MessageBox.Show(string.Format("版本:{0}\r\n安装路径:{1}",officeVersion,officePath));
}
}

//是否安装及版本
private void btnIsInstall_Click(object sender, EventArgs e)
{
try
{
string version = "";
if (OfficeOperator.IsInstall(out version))
{
MessageBox.Show(string.Format("当前安装了{0}", version));
}
else
{
MessageBox.Show("您当前还没有安装微软Office系列软件");
}
}
catch(Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
}

N久么发过贴,冒个泡,发个代码,不妥之处请指正!
...全文
739 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
TGB1989 2011-08-10
  • 打赏
  • 举报
回复
抛出异常:没有该注册表项,那对于没有安装office的机器,我该怎么检测呢?
zwamm 2010-09-15
  • 打赏
  • 举报
回复
呵呵,如果没有安装office就得报异常了……
王子样 2009-12-28
  • 打赏
  • 举报
回复
果然高手啊。
ccaozhiping 2009-04-21
  • 打赏
  • 举报
回复
非常感谢
peterb 2009-04-14
  • 打赏
  • 举报
回复
不错 查注册表应该就获取这些内容
冷月孤峰 2009-04-14
  • 打赏
  • 举报
回复
不错,收藏,备用
outou 2009-04-14
  • 打赏
  • 举报
回复
不错呀。
龙宜坡 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 oyljerry 的回复:]
查看各个版本注册表
是不是应该先查高版本,防止低版本升级到高版本没有删除干净注册表...
[/Quote]

接受建议,谢谢!
oyljerry 2009-04-13
  • 打赏
  • 举报
回复
查看各个版本注册表
是不是应该先查高版本,防止低版本升级到高版本没有删除干净注册表...
辰爸 2009-04-13
  • 打赏
  • 举报
回复
学习,有收获,谢谢发帖!
陌上花花 2009-04-13
  • 打赏
  • 举报
回复
学习过。。谢谢
kkun_3yue3 2009-04-13
  • 打赏
  • 举报
回复
收藏
wuyq11 2009-04-13
  • 打赏
  • 举报
回复
不错,应该用注册表
PandaIT 2009-04-13
  • 打赏
  • 举报
回复
up

再看

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧