jun353835273 发表于 2020-2-21 11:51:00

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

代码没有经过调试直接复制的桌子公司的原帖,主要是为了搜索方便

using Microsoft.Win32;
using System.Reflection;
using Autodesk..Runtime;
using Autodesk.AutoCAD.ApplicationServices;

public void RegisterMyApp()
{
// Get the AutoCAD Applications key
string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
string sAppName = "MyApp";
RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
// Check to see if the "MyApp" key exists
string[] subKeys = regAcadAppKey.GetSubKeyNames();
foreach (string subKey in subKeys)
{
// If the application is already registered, exit
if (subKey.Equals(sAppName))
{
regAcadAppKey.Close();
return;
}
}
// Get the location of this module
string sAssemblyPath = Assembly.GetExecutingAssembly().Location;
// Register the application
RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName);
regAppAddInKey.SetValue("DESCRIPTION", sAppName, RegistryValueKind.String);
regAppAddInKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);
regAppAddInKey.SetValue("LOADER", sAssemblyPath, RegistryValueKind.String);
regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);
regAcadAppKey.Close();
}

public void UnregisterMyApp()
{
// Get the AutoCAD Applications key
string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
string sAppName = "MyApp";
RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);
// Delete the key for the application
regAcadAppKey.DeleteSubKeyTree(sAppName);
regAcadAppKey.Close();
**** Hidden Message *****

zzyong00 发表于 2020-2-22 12:27:00

没有用vb.net编程的吗

mycad 发表于 2020-2-22 17:31:00

RegistryKey是引用的Microsoft.win32.RegistryKey?还是Autodesk.AutoCAD.Runtime.RegistryKey?

mycad 发表于 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
{
      
      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();
      }
      
      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。

jun353835273 发表于 2020-2-23 11:17:00


测试过没有哇
页: [1]
查看完整版本: C# net搬运 启动autocad的时候就加载插件dll