CAD二次开发,安装程序中写注册表

  • CAD二次开发,安装程序中写注册表已关闭评论
  • 195 次浏览
  • A+
所属分类:.NET技术
摘要

我们知道,dll加载到cad中后使用就可以拿到当前cad的注册表,那么如果想在安装程序时写,此时并没有cad的环境,要怎么办呢?


一、加载dll时写注册表

我们知道,dll加载到cad中后使用

HostApplicationServices.Current.RegistryProductRootKey()

就可以拿到当前cad的注册表,那么如果想在安装程序时写,此时并没有cad的环境,要怎么办呢?

二、获取所有已安装的cad的注册表路径

cad在安装后,会在注册表的计算机HKEY_LOCAL_MACHINESOFTWAREAutodeskHardcopy目录下存放所有已安装的cad的注册表位置

CAD二次开发,安装程序中写注册表

如图,由于我只安装了一个,所以这里只显示一个,我们使用代码即可获取到所有的valueName值

public static List<string> GetHardcopyList()     {         List<string> list = new List<string>();         var key = Registry.LocalMachine.CreateSubKey(@"SOFTWAREAutodeskHardcopy");         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路径