通过注册表来检测是否安装Office

C/S项目需求:

检测Office是否安装以及获取安装 路径 及安装版本

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


同理,检测QQ、Fetion、360杀毒、IE浏览器,Chrome、Office 2003/2007/2010

只需修改验证

1 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Tencent\PlatForm_Type_List\3", false);//如果bool值为true则对打开的项进行读写操作,否则为只读打开

即可

检测QQ是否已安装,通过注册表

View Code
 1     #region 检测QQ是否安装
 2     public static bool isInstallQQ(out string QQVersion, out string QQPath)
 3     {
 4         bool result = false;
 5         string str_QQPath = string.Empty;
 6         string str_QQVersion = string.Empty;
 7         QQVersion = string.Empty;
 8         QQPath = string.Empty;
 9 
10         GetQQPath(out str_QQPath, out str_QQVersion);
11         if (!string.IsNullOrEmpty(str_QQPath) && !string.IsNullOrEmpty(str_QQVersion))
12         {
13             result = true;
14             QQVersion = str_QQVersion;
15             QQPath = str_QQPath;
16         }
17         return result;
18     }
19 
20     /// <summary>
21     /// 
22     /// </summary>
23     /// <param name="str_QQPath"></param>
24     /// <param name="str_QQVersion"></param>
25     private static void GetQQPath(out string str_QQPath, out string str_QQVersion)
26     {
27         string str_PatheResult = string.Empty;
28         string str_VersionResult = string.Empty;
29         string str_KeyName = "TypePath";
30         object objResult = null;
31         Microsoft.Win32.RegistryValueKind regValueKind;//指定在注册表中存储值时所用的数据类型,或标识注册表中某个值的数据类型。
32         Microsoft.Win32.RegistryKey regKey = null;//表示 Windows 注册表中的项级节点(注册表对象?)
33         Microsoft.Win32.RegistryKey regSubKey = null;
34         try
35         {
36             regKey = Microsoft.Win32.Registry.LocalMachine;//读取HKEY_LOCAL_MACHINE项
37             if (regSubKey == null)
38             {//QQ
39                 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Tencent\PlatForm_Type_List\3", false);//如果bool值为true则对打开的项进行读写操作,否则为只读打开
40                 str_VersionResult = "QQ";
41                 str_KeyName = "TypePath";
42             }
43             objResult = regSubKey.GetValue(str_KeyName);
44             regValueKind = regSubKey.GetValueKind(str_KeyName);
45             if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
46             {
47                 str_PatheResult = objResult.ToString();
48             }
49         }
50         catch (Exception ex)
51         {
52             LogHelper.WriteLogError(ex.ToString());
53             //throw ex;
54         }
55         finally
56         {
57             if (regKey != null)
58             {
59                 regKey.Close();
60                 regKey = null;
61             }
62 
63             if (regSubKey != null)
64             {
65                 regSubKey.Close();
66                 regSubKey = null;
67             }
68         }
69         str_QQPath = str_PatheResult;
70         str_QQVersion = str_VersionResult;
71     }
72     #endregion

检测迅雷

View Code
 1     #region 检测 Thunder 迅雷
 2     public static bool isInstallThunder(out string thunderVersion, out string thunderPath)
 3     {
 4         bool result = false;
 5         string str_ThunderPath = string.Empty;
 6         string str_ThunderVersion = string.Empty;
 7         thunderVersion = string.Empty;
 8         thunderPath = string.Empty;
 9 
10         GetThunderPath(out str_ThunderPath, out str_ThunderVersion);
11         if (!string.IsNullOrEmpty(str_ThunderPath) && !string.IsNullOrEmpty(str_ThunderVersion))
12         {
13             result = true;
14             thunderVersion = str_ThunderVersion;
15             thunderPath = str_ThunderPath;
16         }
17         return result;
18     }
19 
20     /// <summary>
21     /// 
22     /// </summary>
23     /// <param name="str_QQPath"></param>
24     /// <param name="str_QQVersion"></param>
25     private static void GetThunderPath(out string str_thunderPath, out string str_thunderVersion)
26     {
27         string str_PatheResult = string.Empty;
28         string str_VersionResult = string.Empty;
29         string str_KeyName = "Path";
30         object objResult = null;
31         Microsoft.Win32.RegistryValueKind regValueKind;//指定在注册表中存储值时所用的数据类型,或标识注册表中某个值的数据类型。
32         Microsoft.Win32.RegistryKey regKey = null;//表示 Windows 注册表中的项级节点(注册表对象?)
33         Microsoft.Win32.RegistryKey regSubKey = null;
34         try
35         {
36             regKey = Microsoft.Win32.Registry.LocalMachine;//读取HKEY_LOCAL_MACHINE项
37             if (regSubKey == null)
38             {//QQ
39                 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Thunder Network\ThunderOem\thunder_backwnd", false);//如果bool值为true则对打开的项进行读写操作,否则为只读打开
40                 str_VersionResult = "Thunder";
41                 str_KeyName = "Path";
42             }
43             objResult = regSubKey.GetValue(str_KeyName);
44             regValueKind = regSubKey.GetValueKind(str_KeyName);
45             if (regValueKind == Microsoft.Win32.RegistryValueKind.ExpandString)
46             {
47                 str_PatheResult = objResult.ToString();
48             }
49         }
50         catch (Exception ex)
51         {
52             LogHelper.WriteLogError(ex.ToString());
53         }
54         finally
55         {
56             if (regKey != null)
57             {
58                 regKey.Close();
59                 regKey = null;
60             }
61             if (regSubKey != null)
62             {
63                 regSubKey.Close();
64                 regSubKey = null;
65             }
66         }
67         str_thunderPath = str_PatheResult;
68         str_thunderVersion = str_VersionResult;
69     }
70     #endregion
原文地址:https://www.cnblogs.com/xyzla/p/2586540.html