明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1239|回复: 5

[基础] C# net搬运 启动autocad的时候就加载插件dll

[复制链接]
发表于 2020-2-21 11:51 | 显示全部楼层 |阅读模式
代码没有经过调试直接复制的桌子公司的原帖,主要是为了搜索方便
https://forums.autodesk.com/t5/n ... regkey/td-p/6650480
  1. using Microsoft.Win32;
  2. using System.Reflection;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. [CommandMethod("RegisterMyApp")]
  6. public void RegisterMyApp()
  7. {
  8. // Get the AutoCAD Applications key
  9. string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
  10. string sAppName = "MyApp";
  11. RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
  12. RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
  13. // Check to see if the "MyApp" key exists
  14. string[] subKeys = regAcadAppKey.GetSubKeyNames();
  15. foreach (string subKey in subKeys)
  16. {
  17. // If the application is already registered, exit
  18. if (subKey.Equals(sAppName))
  19. {
  20. regAcadAppKey.Close();
  21. return;
  22. }
  23. }
  24. // Get the location of this module
  25. string sAssemblyPath = Assembly.GetExecutingAssembly().Location;
  26. // Register the application
  27. RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName);
  28. regAppAddInKey.SetValue("DESCRIPTION", sAppName, RegistryValueKind.String);
  29. regAppAddInKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);
  30. regAppAddInKey.SetValue("LOADER", sAssemblyPath, RegistryValueKind.String);
  31. regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);
  32. regAcadAppKey.Close();
  33. }
  34. [CommandMethod("UnregisterMyApp")]
  35. public void UnregisterMyApp()
  36. {
  37. // Get the AutoCAD Applications key
  38. string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
  39. string sAppName = "MyApp";
  40. RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
  41. RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
  42. // Delete the key for the application
  43. regAcadAppKey.DeleteSubKeyTree(sAppName);
  44. regAcadAppKey.Close();
发表于 2020-2-22 12:27 | 显示全部楼层
没有用vb.net编程的吗
发表于 2020-2-22 17:31 | 显示全部楼层
RegistryKey是引用的Microsoft.win32.RegistryKey?还是Autodesk.AutoCAD.Runtime.RegistryKey?
发表于 2020-2-23 11:08 | 显示全部楼层
https://www.jianshu.com/p/6196fd55b75f
过修改注册表的方式实现.net程序的自动加载,注册程序的位置可以写入到计算机\HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R20.1\ACAD-F001:804\Applications目录下,或者计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R20.1\ACAD-F001:804\Applications下 ,其中R20.1为AutoCad内部版本号,804代表中文版本。
namespace Demo1
  public class Hello
  {
      [CommandMethod("RegisterMyApp")]
      public void RegisterMyApp()
      {
          // 获取Applications在注册表中的key
          string appKey = HostApplicationServices.Current.MachineRegistryProductRootKey;
          string sAppName = "RegDemo";
          RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(appKey);
          RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
          //检查RegDemo键是否存在
          string[] subKeys = regAcadAppKey.GetSubKeyNames();
          foreach (string subKey in subKeys)
          {
              // 如果存在就退出
              if (subKey.Equals(sAppName))
              {
                  regAcadAppKey.Close();
                  return;
              }
          }
          // 获取生成的dll文件位置
          string sAssemblyPath = Assembly.GetExecutingAssembly().Location;
          // 注册程序
          RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName);
          regAppAddInKey.SetValue("DESCRIPTION", sAppName, Microsoft.Win32.RegistryValueKind.String);
          regAppAddInKey.SetValue("LOADCTRLS", 14, Microsoft.Win32.RegistryValueKind.DWord);
          regAppAddInKey.SetValue("LOADER", sAssemblyPath, Microsoft.Win32.RegistryValueKind.String);
          regAppAddInKey.SetValue("MANAGED", 1, Microsoft.Win32.RegistryValueKind.DWord);
          regAcadAppKey.Close();
      }
      [CommandMethod("UnregisterMyApp")]
      public void UnregisterMyApp()
      {
          //计算机\HKEY_CURRENT_USER 下Software\Autodesk\AutoCAD\R20.1\ACAD-F001:804
          string sProdKey = HostApplicationServices.Current.MachineRegistryProductRootKey;
          string sAppName = "RegDemo";
          RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
          RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
          //删除
          regAcadAppKey.DeleteSubKeyTree(sAppName);
          regAcadAppKey.Close();
      }
程序运行后,首次启动AutoCad,执行netload命令加载dll后运行RegisterMyApp命令后,下次启动后,dll会自动加载,同时注册表中会有相应的项。如下图所示:
注册表运行效果
注册表运行效果
执行UnregisterMyApp命令后可以删除RegDemo注册表的相关内容。
说明:
HostApplicationServices.Current.MachineRegistryProductRootKey返回的结果指向计算机\HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R20.1\ACAD-F001:804。
 楼主| 发表于 2020-2-23 11:17 | 显示全部楼层
mycad 发表于 2020-2-23 11:08
https://www.jianshu.com/p/6196fd55b75f
过修改注册表的方式实现.net程序的自动加载,注册程序的位置可以 ...

测试过没有哇
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-3 03:30 , Processed in 0.286473 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表