123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /* ==============================================================================
- * 功能描述:stringUtility
- * 创 建 者:Garrett
- * 创建日期:2019/8/1 11:14:34
- * ==============================================================================*/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- using Microsoft.Win32;
- namespace SAGA.DotNetUtils.Revit
- {
- /// <summary>
- /// Revit版本信息
- /// </summary>
- public class RevitProductUtility
- {
- [DllImport("msi.dll", CharSet = CharSet.Unicode)]
- public static extern uint MsiEnumClients(string szComponent, uint iProductIndex, string lpProductBuf);
- [DllImport("msi.dll", CharSet = CharSet.Unicode)]
- public static extern int MsiGetProductInfo(string product, string property, string valueBuf, ref int len);
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern int RegCloseKey(UIntPtr hKey);
- [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, ref UIntPtr hkResult);
- [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, ref uint lpData, ref uint lpcbData);
- [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, StringBuilder lpData, ref uint lpcbData);
- /// <summary>
- /// 获取所有安装的Revit版本
- /// </summary>
- /// <returns>
- /// 2018,E:\Program Files\Autodesk\Revit 2018\
- /// </returns>
- public static List<Tuple<string, string>> GetAllInstalledRevitProducts()
- {
- List<Tuple<string, string>> list = new List<Tuple<string, string>>();
- initializeProductsDictionary();
- try
- {
- List<string>.Enumerator enumerator = GetInstalledCodes().GetEnumerator();
- while (enumerator.MoveNext())
- {
- Tuple<string, string> item = GetProductVersionInfo(enumerator.Current);
- if (null != item)
- {
- list.Add(item);
- }
- }
- }
- catch (Exception)
- {
- }
- return list;
- }
- private static Tuple<string, string> getInstalledProductInfo(string productRegGUID)
- {
- Tuple<string, string> tuple = null;
- string installPath, version;
- uint num4 = 0;
- if (!m_productsHashtable.ContainsKey(productRegGUID))
- {
- return null;
- }
- RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall").OpenSubKey(productRegGUID);
- if (null != key)
- {
- installPath = (string)key.GetValue("InstallLocation");
- version = (string)key.GetValue("DisplayName");
- tuple = new Tuple<string, string>(version, installPath);
- goto Label_0365;
- }
- UIntPtr hkResult = new UIntPtr();
- uint lpcbData = 0x400;
- uint lpData = 0;
- StringBuilder builder = new StringBuilder(0x400);
- RegQueryValueEx(hkResult, "InstallLocation", 0, ref num4, builder, ref lpcbData);
- installPath = builder.ToString();
- RegQueryValueEx(hkResult, "DisplayName", 0, ref num4, builder, ref lpcbData);
- version = builder.ToString();
- tuple = new Tuple<string, string>(version, installPath);
- return tuple;
- Label_0365:
- key.Close();
- return tuple;
- }
- private static List<string> GetInstalledCodes()
- {
- List<string> list = new List<string>();
- string lpProductBuf = new string('0', 0x26);
- string szComponent = "{DF7D485F-B8BA-448E-A444-E6FB1C258912}";
- uint num = 1;
- if (MsiEnumClients(szComponent, 0, lpProductBuf) == 0)
- {
- uint num2;
- do
- {
- string item = string.Copy(lpProductBuf);
- list.Add(item);
- num2 = num;
- num++;
- }
- while (MsiEnumClients(szComponent, num2, lpProductBuf) == 0);
- }
- return list;
- }
- private static Tuple<string, string> GetProductVersionInfo(string productCode)
- {
- Match match = new Regex(@"^(\{{0,1}(7346B4A[0-9a-fA-F])-(?<Majorversion>([0-9a-fA-F]){2})(?<Subversion>([0-9a-fA-F]){2})-(?<Discipline>([0-9a-fA-F]){2})(?<Platform>([0-9a-fA-F]){1})[0-9a-fA-F]-(?<Language>([0-9a-fA-F]){4})-705C0D862004\}{0,1})$").Match(productCode);
- if (!match.Success)
- {
- return getInstalledProductInfo(productCode);
- }
- string version = "20" + match.Result("${Majorversion}");
- string installPath = new string(' ', 260);
- int num4 = 260;
- MsiGetProductInfo(productCode, "InstallLocation", installPath, ref num4);
- return new Tuple<string, string>(version, installPath.Substring(0,num4));
- }
- private static Hashtable m_productsHashtable;
- private static void initializeProductsDictionary()
- {
- if (null == m_productsHashtable)
- {
- m_productsHashtable = new Hashtable();
- }
- else
- {
- m_productsHashtable.Clear();
- }
- }
- }
- }
|