通过RevitAddInUtility.dll提供的方法,我们很容易获取到一些本地Revit安装的信息,比如安装的版本,安装路径,和插件配置文件所在的文件夹等。
但如果你对代码有洁癖,觉得为了这么点事儿,没必要去引用一个外部Dll。或者其他别的什么原因都可以,导致不想去引用
RevitAddInUtility.dll这个类库,同时又想获取到Revit安装的相关信息怎么办呢?我们可以通过读取注册表来获取我们想要的信息。
Revit安装信息,在注册表中按如下键值存放。
我给出了一种获取Revit安装信息的方法。其中包括两个类,存放Revit安装信息的RevitItem类,和构建RevitItem信息的RevitManager类。如果有需要的话可以直接拷贝到项目中,直接运行即可
RevitItem类
/// <summary>
/// Revit安装信息
/// </summary>
public class RevitItem
{
internal RevitItem(string name,string revitFile)
{
Name = name;
RevitFile = revitFile;
AllUsersAddInFolder = RevitManager.GetAllUsersAddInFolder(name);
CurrentUserAddInFolder= RevitManager.GetCurrentUserAddInFolder(name);
}
/// <summary>
/// Revit名称(2018,2019)
/// </summary>
public string Name { get; private set; }
/// <summary>
/// revit.exe所在位置
/// </summary>
public string RevitFile { get;private set; }
/// <summary>
/// 所有用户插件目录
/// </summary>
public string AllUsersAddInFolder { get; private set; }
/// <summary>
/// 当前用户插件目录
/// </summary>
public string CurrentUserAddInFolder { get; private set; }
}
RevitManager类
/// <summary>
/// revit相关管理
/// </summary>
public class RevitManager
{
#region 私有设置
private const string RevitNameMatch = @"^20\d{2}$";
private const string ExeName = "Revit.exe";
private static Dictionary<string, RevitItem> m_DicRevitItems = new Dictionary<string, RevitItem>();
static RevitManager()
{
LoadRevitItems();
}
/// <summary>
/// 初始化所有安装revit项目
/// </summary>
private static void LoadRevitItems()
{
List<RevitItem> revitItems = new List<RevitItem>();
string baseKey = string.Format("SOFTWARE\\Autodesk\\Revit");
//LocalMachine key值
var useRegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
var localMachineKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, useRegistryView);
#region 查找注册表安装路径
using (RegistryKey registryKey = localMachineKey.OpenSubKey(baseKey))
{
if (registryKey != null)
{
//找到形如(2018,2019)的键值
var revitVersions = registryKey.GetSubKeyNames().Where(n => CheckRevitName(n)).ToList();
foreach (var version in revitVersions)
{
var versionKey = registryKey.OpenSubKey(version);
//找到包含REVIT名称的键值
var subKeyName = versionKey.GetSubKeyNames().FirstOrDefault(n => n.ToUpper().StartsWith("REVIT"));
if (string.IsNullOrWhiteSpace(subKeyName))
{
continue;
}
var tempKey = versionKey.OpenSubKey(subKeyName);
if (tempKey != null)
{
#region 初始化RevitItem
var installationLocation = tempKey.GetValue("InstallationLocation") as string ?? string.Empty;
var revitFile = Path.Combine(installationLocation, ExeName);
if (File.Exists(revitFile))
{
RevitItem item = new RevitItem(version, revitFile);
revitItems.Add(item);
}
#endregion
}
}
registryKey.Close();
}
}
#endregion
revitItems.ForEach(revit => m_DicRevitItems[revit.Name]=revit);
}
#endregion
/// <summary>
/// 获取所有Revit项目
/// </summary>
/// <returns></returns>
public static List<RevitItem> GetRevitItems()
{
return m_DicRevitItems.Values.ToList();
}
/// <summary>
/// 获取指定名称的Revit项目
/// </summary>
/// <param name="revitName"></param>
/// <returns></returns>
public static RevitItem GetRevitItem(string revitName)
{
m_DicRevitItems.TryGetValue(revitName, out RevitItem revitItem);
return revitItem;
}
/// <summary>
/// 检查Revit名称格式是否正确
/// </summary>
/// <param name="revitName"></param>
/// <returns></returns>
public static bool CheckRevitName(string revitName)
{
return Regex.IsMatch(revitName, RevitNameMatch);
}
/// <summary>
/// 获取插件放置公共目录
/// </summary>
/// <param name="revitItemName"></param>
/// <returns></returns>
public static string GetAllUsersAddInFolder(string revitItemName)
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Autodesk\Revit\AddIns", revitItemName);
}
/// <summary>
/// 获取插件放置当前用户目录
/// </summary>
/// <param name="revitItemName"></param>
/// <returns></returns>
public static string GetCurrentUserAddInFolder(string revitItemName)
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Autodesk\Revit\AddIns", revitItemName);
}
}
使用的话直接调用var items = RevitManager.GetRevitItems();即可。
来源:CSDN
作者:临风小红楼
链接:https://blog.csdn.net/xuhuo1234/article/details/101559865