最近工作中遇到的一些问题,我将都记录在sina博客,成为技术博文,以便日后查阅,当然也欢迎各位网友进行拍砖练习.
代码如下:
View Code
1 publicclass TestOffice
2 {
3 public TestOffice()
4 { }
5 ///<summary>
6 /// 检测是否安装office
7 ///</summary>
8 ///<param name="str_Version"> 获得并返回安装的office版本</param>
9 ///<returns></returns>
10 publicstaticbool IsInstall(outstring 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 privatestaticvoid GetOfficePath(outstring str_OfficePath, outstring 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 }
2 {
3 public TestOffice()
4 { }
5 ///<summary>
6 /// 检测是否安装office
7 ///</summary>
8 ///<param name="str_Version"> 获得并返回安装的office版本</param>
9 ///<returns></returns>
10 publicstaticbool IsInstall(outstring 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 privatestaticvoid GetOfficePath(outstring str_OfficePath, outstring 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 }