CAD工具之家's Archivers

From boitboy on 2013-06-23 16:40:54

枚举电脑上安装的所有CAD产品

枚举本机安装的所有CAD产品软件下载 运行效果: 代码: //AcadProduct.cs using System; using System.Collections.Generic; using System.Text; namespace Cadgj.Com.AllInstallCad {     /// <summary>     /// CAD产品     /// </summary>     public class AcadProduct     {         private string _strVer = null;         /// <summary>         /// CAD版本号(如R16.1)         /// </summary>         public string strVer         {             get             {                 return _strVer;             }             set             {                 _strVer = value;             }         }         private string _strKey = null;         /// <summary>         /// CAD内部识别KEY(如ACAD-301:804)         /// </summary>         public string strKey         {             get             {                 return _strKey;             }             set             {                 _strKey = value;             }         }         private string _strProductName = null;         /// <summary>         /// CAD产品名称(如AutoCAD 2005)         /// </summary>         public string strProductName         {             get             {                 return _strProductName;             }             set             {                 _strProductName = value;             }         }         private string _strLanguage = null;         /// <summary>         /// 语言(如Simplified Chinese)         /// </summary>         public string strLanguage         {             get             {                 return _strLanguage;             }             set             {                 _strLanguage = value;             }         }         private string _strLangAbbrev = null;         /// <summary>         /// 语言(简写)(如chs)         /// </summary>         public string strLangAbbrev         {             get             {                 return _strLangAbbrev;             }             set             {                 _strLangAbbrev = value;             }         }         private string _strAcadLocation = null;         /// <summary>         /// 安装路径(如D:\Program Files\AutoCAD 2005)         /// </summary>         public string strAcadLocation         {             get             {                 return _strAcadLocation;             }             set             {                 _strAcadLocation = value;             }             }         private string _strRegLocation;         /// <summary>         /// 注册表路径(如SOFTWARE\Autodesk\AutoCAD\R16.1\ACAD-301:804)         /// </summary>         public string strRegLocation         {             get             {                 return _strRegLocation;             }             set             {                 _strRegLocation = value;             }         }         private bool _bX64 = false;         /// <summary>         /// 是否是64位版本         /// </summary>         public bool bX64         {             get             {                 return _bX64;             }             set             {                 _bX64 = value;             }         }         public override string ToString()         {             return strProductName + "(" + strLangAbbrev + ")";         }     } } //AcadProducts.cs using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.IO; namespace Cadgj.Com.AllInstallCad {     /// <summary>     /// CAD产品列表     /// </summary>     public class AcadProducts     {         /// <summary>         /// 读取CAD产品         /// </summary>         /// <param name="subKey"></param>         /// <param name="strCadVer"></param>         /// <param name="pszName"></param>         /// <param name="strSubKey"></param>         /// <param name="bX64"></param>         /// <returns></returns>         private AcadProduct ReadProduct(RegistryKey subKey, string strCadVer, string pszName, string strSubKey, bool bX64)         {             if (subKey == null)                 return null;             string lpProductName = subKey.GetValue("ProductName").ToString();             string lpAcadLocation = subKey.GetValue("AcadLocation").ToString();             string lpLanguage = subKey.GetValue("Language").ToString();             string lpLangAbbrev = subKey.GetValue("LangAbbrev").ToString();             if (string.IsNullOrEmpty(lpAcadLocation))             {                 return null;             }             if ((!File.Exists(Path.Combine(lpAcadLocation, "acad.exe"))) || (string.IsNullOrEmpty(lpProductName)))             {                 return null;             }             AcadProduct profile = new AcadProduct();             profile.strVer = strCadVer;             profile.strKey = pszName;             profile.strProductName = lpProductName;             profile.strLanguage = lpLanguage;             profile.strLangAbbrev = lpLangAbbrev;             profile.strAcadLocation = lpAcadLocation;             profile.strRegLocation = strSubKey;             profile.bX64 = bX64;             return profile;         }         /// <summary>         /// 读取CAD产品列表         /// </summary>         /// <param name="key"></param>         /// <param name="strCadVer"></param>         /// <param name="strKey"></param>         /// <param name="bX64"></param>         /// <returns></returns>         private List<AcadProduct> ReadProducts(RegistryKey key, string strCadVer, string strKey,bool bX64)         {             if (key == null)             {                 return null;             }             List<AcadProduct> apArray = new List<AcadProduct>();             foreach (string pszName in key.GetSubKeyNames())             {                 try                 {                     string strSubKey = strKey + "\\" + pszName;                     RegistryKey subKey = key.OpenSubKey(pszName);                     if (subKey == null)                         continue;                     AcadProduct ap = ReadProduct(subKey,strCadVer,pszName,strSubKey,bX64);                     if (ap != null)                     {                         apArray.Add(ap);                     }                     subKey.Close();                 }                 catch                 {                 }             }             return apArray;         }         /// <summary>         /// 读取所有安装的32位版本的CAD         /// </summary>         /// <param name="verArray"></param>         /// <returns></returns>         public List<AcadProduct> EnumAcadProductsX86(IList<string> verArray)         {             List<AcadProduct> apArray = new List<AcadProduct>();             try             {                 for (int i = 0; i < verArray.Count; i++)                 {                     string strCadVer = verArray[i];                     // 查找AutoCAD系列产品                     //string strKey = string.Format("Software\\Autodesk\\AutoCAD\\{0}", strCadVer);                     string strKey = string.Format("Software\\Autodesk\\AutoCAD\\{0}", strCadVer);                     RegistryKey key = X64RegistryKeyHelper.OpenRegistryKey(X64RegistryKeyHelper.HKEY_LOCAL_MACHINE, strKey);                     if (key == null)                     {                         continue;                     }                     List<AcadProduct> apArray1 = ReadProducts(key, strCadVer, strKey, false);                     if (apArray1 != null)                     {                         apArray.AddRange(apArray1);                     }                 }                 return apArray;             }             catch             {                 return apArray;             }         }         /// <summary>         /// 读取所有安装的64位版本的CAD(32位系统调用会返回空的列表)         /// </summary>         /// <param name="verArray"></param>         /// <returns></returns>         public List<AcadProduct> EnumAcadProductsX64(IList<string> verArray)         {             List<AcadProduct> apArray = new List<AcadProduct>();             if (IntPtr.Size == 8)             {                 try                 {                     for (int i = 0; i < verArray.Count; i++)                     {                         string strCadVer = verArray[i];                         // 查找AutoCAD系列产品                         //string strKey = string.Format("Software\\Autodesk\\AutoCAD\\{0}", strCadVer);                         string strKey = string.Format("Software\\Autodesk\\AutoCAD\\{0}", strCadVer);                         RegistryKey key = Registry.LocalMachine;                         key = key.OpenSubKey(strKey);                         if (key == null)                         {                             continue;                         }                         List<AcadProduct> apArray1 = ReadProducts(key, strCadVer, strKey, false);                         if (apArray1 != null)                         {                             apArray.AddRange(apArray1);                         }                     }                     return apArray;                 }                 catch                 {                     return apArray;                 }             }             else             {                 return apArray;             }         }     } } //X64RegistryKeyHelper.cs using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using Microsoft.Win32; namespace Cadgj.Com.AllInstallCad {     /// <summary>     /// 读取64位操作系统的32位注册表辅助类     /// </summary>     public static class X64RegistryKeyHelper     {         public const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;         public const int READ_CONTROL = 0x00020000;         public const int SYNCHRONIZE = 0x00100000;         public const int STANDARD_RIGHTS_READ = READ_CONTROL;         public const int STANDARD_RIGHTS_WRITE = READ_CONTROL;         public const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;         public const int STANDARD_RIGHTS_ALL = 0x001F0000;         public const int KEY_QUERY_VALUE = (0x0001);         public const int KEY_SET_VALUE = (0x0002);         public const int KEY_CREATE_SUB_KEY = (0x0004);         public const int KEY_ENUMERATE_SUB_KEYS = (0x0008);         public const int KEY_NOTIFY = (0x0010);         public const int KEY_CREATE_LINK = (0x0020);         public const int KEY_WOW64_32KEY = (0x0200);         public const int KEY_WOW64_64KEY = (0x0100);         public const int KEY_WOW64_RES = (0x0300);         public const int KEY_READ = ((STANDARD_RIGHTS_READ |             KEY_QUERY_VALUE |             KEY_ENUMERATE_SUB_KEYS             | KEY_NOTIFY)             &             (~SYNCHRONIZE));         public const int KEY_WRITE = ((STANDARD_RIGHTS_WRITE |             KEY_SET_VALUE |             KEY_CREATE_SUB_KEY)             &             (~SYNCHRONIZE));         public const int KEY_EXECUTE = ((KEY_READ)             &             (~SYNCHRONIZE));         public const int KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL |                                           KEY_QUERY_VALUE |                                           KEY_SET_VALUE |                                           KEY_CREATE_SUB_KEY |                                           KEY_ENUMERATE_SUB_KEYS |                                           KEY_NOTIFY |                                           KEY_CREATE_LINK)                                           &                                          (~SYNCHRONIZE));         static UIntPtr HKEY_CLASSES_ROOT = (UIntPtr)0x80000000;         static UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;         static UIntPtr HKEY_USERS = (UIntPtr)0x80000003;         static UIntPtr HKEY_CURRENT_CONFIG = (UIntPtr)0x80000005;         public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);         [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]         static extern int RegOpenKeyEx(             UIntPtr hKey,             string subKey,             uint options,             int sam,             out IntPtr phkResult);         private static SafeHandle CreateRegistrySafeHandle(IntPtr handle)         {             Assembly ass = typeof(SafeHandle).Assembly;             Type type = ass.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");             SafeHandle sh = (SafeHandle)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { handle, true }, null);             return sh;         }         private static FieldInfo GetHkeyField()         {             Type type = typeof(RegistryKey);             FieldInfo info = type.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);             return info;         }         public static RegistryKey OpenRegistryKey(UIntPtr rootKey, string strKey)         {             FieldInfo hkeyInfo = GetHkeyField();             IntPtr hTargetKey = IntPtr.Zero;             int l = RegOpenKeyEx(rootKey, strKey, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_32KEY, out hTargetKey);             if (l != 0)             {                 return null;             }             SafeHandle sh = CreateRegistrySafeHandle(hTargetKey);             RegistryKey targetKey = (RegistryKey)Activator.CreateInstance(typeof(RegistryKey), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { sh, true }, null);             return targetKey;         }     } } VS工程下载:Cadgj.Com.AllInstallCad

查看完整版本: 枚举电脑上安装的所有CAD产品

Tags:


©CAD工具之家
创办于:2013年5月24日