乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 104|回复: 4

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

[复制链接]

20

主题

121

帖子

11

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
201
发表于 2020-2-21 11:51:00 | 显示全部楼层 |阅读模式
代码没有经过调试直接复制的桌子公司的原帖,主要是为了搜索方便
  1. using Microsoft.Win32;
  2. using System.Reflection;
  3. using Autodesk..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();

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

23

主题

561

帖子

13

银币

中流砥柱

Rank: 25

铜币
653
发表于 2020-2-22 12:27:00 | 显示全部楼层
没有用vb.net编程的吗
回复

使用道具 举报

110

主题

324

帖子

10

银币

中流砥柱

Rank: 25

铜币
764
发表于 2020-2-22 17:31:00 | 显示全部楼层
RegistryKey是引用的Microsoft.win32.RegistryKey?还是Autodesk.AutoCAD.Runtime.RegistryKey?
回复

使用道具 举报

110

主题

324

帖子

10

银币

中流砥柱

Rank: 25

铜币
764
发表于 2020-2-23 11:08:00 | 显示全部楼层
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。
回复

使用道具 举报

20

主题

121

帖子

11

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
201
发表于 2020-2-23 11:17:00 | 显示全部楼层

测试过没有哇
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:22 , Processed in 0.611885 second(s), 73 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表