定义一个函数,随AutoCAD 启动加载当前程序集到autocad,涉及到写入注册表,注意这是在autocad内部加载dll之后处理的方法。。。。
写入HKLM表示所有登录的用户都会受影响(autocad 需要以管理员身份启动才能成功写入注册表),写入HKCU表示当前登录的用户才会收影响
"LOADCTRLS":控制程序随CAD加载的方式,设为Ox02随CAD启动一起加载;
"LOADER":告诉CAD所要加载的程序的路径;
"MANAGED":设为Ox01,告诉CAD这是托管程序。
注意32为cad在64为的电脑上注册表如果写入HKLM 会用不同的路径在“HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\”
注意32为cad在64为的电脑上注册表如果写入HKCU 会用不同的路径在“HKEY_LOCAL_MACHINE\SOFTWARE\"
1 ‘‘‘ <summary> 2 ‘‘‘ 将程序集随acad启动注册表写入注册表 3 ‘‘‘ </summary> 4 ‘‘‘ <param name="keyLocal">HKLM,HKCU</param> 5 Private Shared Sub DllAutoLoader(keyLocal As RegistryKey) 6 7 Dim regPath As String = HostApplicationServices.Current.RegistryProductRootKey ‘Software\Autodesk\AutoCAD\R18.0\ACAD-8001:804 8 Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor 9 Dim assName = Assembly.GetExecutingAssembly().CodeBase 10 Dim apps_Acad = keyLocal.OpenSubKey(Path.Combine(regPath, "Applications"), False) 11 Dim counts = apps_Acad.SubKeyCount, flag As Boolean = False 12 For Each item As String In apps_Acad.GetSubKeyNames() 13 If item = Path.GetFileNameWithoutExtension(assName) Then flag = True 14 Next 15 If flag = False Then 16 Dim autocad_App As Microsoft.Win32.RegistryKey 17 autocad_App = keyLocal.OpenSubKey(Path.Combine(regPath, "Applications"), True) ‘RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl 18 Dim myNerLoader = autocad_App.CreateSubKey(Path.GetFileNameWithoutExtension(assName), RegistryKeyPermissionCheck.Default) 19 myNerLoader.SetValue("DESCRIPTION", "加载自定义dll文件", Microsoft.Win32.RegistryValueKind.String) 20 myNerLoader.SetValue("LOADCTRLS", 2, Microsoft.Win32.RegistryValueKind.DWord) 21 myNerLoader.SetValue("LOADER", assName, Microsoft.Win32.RegistryValueKind.String) 22 myNerLoader.SetValue("MANAGED", 1, Microsoft.Win32.RegistryValueKind.DWord) 23 Application.ShowAlertDialog(Path.GetFileNameWithoutExtension(assName) + "程序自动加载完成,重启AutoCAD 生效!" + Environment.NewLine + "注册表地址为:" + myNerLoader.Name) 24 Else 25 Application.ShowAlertDialog(Path.GetFileNameWithoutExtension(assName) + "程序自动加载完成,无需重复加载!") 26 End If 27 End Sub