世道太乱丶 发表于 2023-4-11 20:34:20

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路径

来源:https://www.cnblogs.com/d1742647821/archive/2023/04/11/17306914.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: CAD二次开发,安装程序中写注册表