C# 通过注册表检测是否安装office组件

最近工作中遇到的一些问题,我将都记录在sina博客,成为技术博文,以便日后查阅,当然也欢迎各位网友进行拍砖练习.

代码如下:

View Code
1 public class TestOffice
2 {
3 public TestOffice()
4 { }
5 /// <summary>
6 /// 检测是否安装office
7 /// </summary>
8 /// <param name="str_Version"> 获得并返回安装的office版本 </param>
9 /// <returns></returns>
10   public static bool IsInstall( out string str_Version)
11 {
12 bool result = false ;
13 string str_OfficePath = string .Empty;
14 string str_OfficeVersion = string .Empty;
15 str_Version = string .Empty;
16
17 GetOfficePath( out str_OfficePath, out str_OfficeVersion);
18 if ( ! string .IsNullOrEmpty(str_OfficePath) &&! string .IsNullOrEmpty(str_OfficeVersion))
19 {
20 result = true ;
21 str_Version = str_OfficeVersion;
22 }
23
24 return result;
25 }
26
27 /// <summary>
28 /// 获取并返回当前安装的office版本和安装路径
29 /// </summary>
30 /// <param name="str_OfficePath"> office的安装路径 </param>
31 /// <param name="str_OfficeVersion"> office的安装版本 </param>
32   private static void GetOfficePath( out string str_OfficePath, out string str_OfficeVersion)
33 {
34 string str_PatheResult = string .Empty;
35 string str_VersionResult = string .Empty;
36 string str_KeyName = " Path " ;
37 object objResult = null ;
38 Microsoft.Win32.RegistryValueKind regValueKind; // 指定在注册表中存储值时所用的数据类型,或标识注册表中某个值的数据类型。
39   Microsoft.Win32.RegistryKey regKey = null ; // 表示 Windows 注册表中的项级节点(注册表对象?)
40   Microsoft.Win32.RegistryKey regSubKey = null ;
41 try
42 {
43 regKey = Microsoft.Win32.Registry.LocalMachine; // 读取HKEY_LOCAL_MACHINE项
44   if (regSubKey == null )
45 { // office97
46   regSubKey = regKey.OpenSubKey( @" SOFTWARE\Microsoft\Office\8.0\Common\InstallRoot " , false ); // 如果bool值为true则对打开的项进行读写操作,否则为只读打开
47 str_VersionResult = " Office97 " ;
48 str_KeyName = " OfficeBin " ;
49 }
50 if (regSubKey == null )
51 { // Office2000
52 regSubKey = regKey.OpenSubKey( @" SOFTWARE\Microsoft\Office\9.0\Common\InstallRoot " , false );
53 str_VersionResult = " Pffice2000 " ;
54 str_KeyName = " Path " ;
55 }
56 if (regSubKey == null )
57 { // officeXp
58 regSubKey = regKey.OpenSubKey( @" SOFTWARE\Microsoft\Office\10.0\Common\InstallRoot " , false );
59 str_VersionResult = " OfficeXP " ;
60 str_KeyName = " Path " ;
61 }
62
63 if (regSubKey == null )
64 { // Office2003
65 regSubKey = regKey.OpenSubKey( @" SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot " , false );
66 str_VersionResult = " Office2003 " ;
67 str_KeyName = " Path " ;
68 }
69
70 if (regSubKey == null )
71 { // office2007
72 regSubKey = regKey.OpenSubKey( @" SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot " , false );
73 str_VersionResult = " Office2007 " ;
74 str_KeyName = " Path " ;
75 }
76 objResult = regSubKey.GetValue(str_KeyName);
77 regValueKind = regSubKey.GetValueKind(str_KeyName);
78 if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
79 {
80 str_PatheResult = objResult.ToString();
81 }
82
83 }
84 catch (Exception ex)
85 {
86
87 throw ex;
88 }
89 finally
90 {
91
92 if (regKey != null )
93 {
94 regKey.Close();
95 regKey = null ;
96 }
97
98 if (regSubKey != null )
99 {
100 regSubKey.Close();
101 regSubKey = null ;
102 }
103 }
104 str_OfficePath = str_PatheResult;
105 str_OfficeVersion = str_VersionResult;
106
107
108 }
109 }

 

  

 

转载于:https://www.cnblogs.com/BreatheLee/archive/2011/05/16/2047675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值