CAD二次開發,安裝程序中寫注冊表
一、加載dll時寫注冊表
我們知道,dll加載到cad中后使用
HostApplicationServices.Current.RegistryProductRootKey()
就可以拿到當前cad的注冊表,那么如果想在安裝程序時寫,此時并沒有cad的環境,要怎么辦呢?
二、獲取所有已安裝的cad的注冊表路徑
cad在安裝后,會在注冊表的計算機\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Hardcopy目錄下存放所有已安裝的cad的注冊表位置

如圖,由于我只安裝了一個,所以這里只顯示一個,我們使用代碼即可獲取到所有的valueName值
public static List<string> GetHardcopyList()
{
List<string> list = new List<string>();
var key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Autodesk\Hardcopy");
if (key != null)
{
string[] subKeyNames = key.GetValueNames();
subKeyNames.Count().Prompt();
foreach (string name in subKeyNames)
{
list.Add(name);
}
}
return list;
}
拿到valueName值后,我們可以用如下方法寫上注冊表
public static void WriteZcb()
{
var names=GetHardcopyList();
var dllFile = "D:\\123.dll";
foreach (var name in names)
{
var address = "SOFTWARE\\" + name + "\\Applications";
RegisteringCAD(address, dllFile);
}
}
/// <summary>
/// 注冊dll
/// </summary>
/// <param name="dllFile">dll文件路徑</param>
/// <returns></returns>
public static bool RegisteringCAD(string address,string dllFile)
{
RegistryKey user = Registry.CurrentUser.OpenSubKey(address, true);
if (user == null)
{
return false;
}
RegistryKey keyUserApp = user.CreateSubKey(Path.GetFileNameWithoutExtension(dllFile));
keyUserApp.SetValue("DESCRIPTION", Path.GetFileNameWithoutExtension(dllFile), RegistryValueKind.String);
keyUserApp.SetValue("LOADCTRLS", 2, RegistryValueKind.DWord);
keyUserApp.SetValue("LOADER", dllFile, RegistryValueKind.String);
keyUserApp.SetValue("MANAGED", 1, RegistryValueKind.DWord);
return true;
}
其中 dllFile為要寫入的dll路徑

浙公網安備 33010602011771號