1. 程式人生 > 其它 >C# AutoCAD為外掛建立桌面和開始選單啟動方式

C# AutoCAD為外掛建立桌面和開始選單啟動方式

先看效果

整體思路,

1.建立快捷方式

 

檢視程式碼

public class AcadStartUpHelper
    {
        ///https://hfsoft.blog.csdn.net/article/details/90166607?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPayColumn-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPayColumn-1.pc_relevant_default&utm_relevant_index=1
        /// <summary>
        /// 啟動程式後,請指定要執行的指令碼(b 代表批處理)。如果指令碼檔案位於“起始”資料夾中,則該指令碼檔案的完整路徑是必需的,除非提供 /six 命令列開關或將 LEGACYCODESEARCH 系統變數設定為 1 來禁止此安全措施。指令碼可以用於設定新圖形檔案中的圖形引數。假定 SCR 檔案型別。
        /// </summary>
        public const string 指令碼名 = "/b ";
        /// <summary>
        /// 啟動時不顯示徽標螢幕。
        /// </summary>
        public const string 無產品徽標螢幕 = "/nologo ";
        /// <summary>
        /// 定支援資料夾,而不是當前資料夾。圖形支援檔案包括字型、選單、AutoLISP 檔案、線型和填充圖案。可以在路徑中指定的資料夾的最大數目是 15。每一資料夾名用分號分隔。
        /// </summary>
        public const string 支援資料夾 = "/s ";
        /// <summary>
        /// 指定啟動時應恢復已載入的 CUIx 檔案中的哪個工作空間。
        /// </summary>
        public const string 預設工作空間 = "/w ";
        /// <summary>
        /// 基於樣板或原型圖形建立新圖形。假定 DWT 檔案型別。
        /// </summary>
        public const string 樣板檔名 = "/t ";
        /// <summary>
        ///指定圖形的指定檢視,以便在啟動時顯示。
        /// </summary>
        public const string 檢視名 = "/v ";
        /// <summary>
        /// 啟動時禁用硬體加速。
        /// </summary>
        public const string 禁用硬體加速 = "/nohardware ";
        /// <summary>
        /// 在當前任務中禁止所有可執行檔案的載入和執行。此開關可用於停止和定位惡意可執行程式碼。注意:但它也會阻止 Express Tools 和某些 AutoCAD 命令工具的執行,因此此開關應僅在緊急情況下使用。
        /// </summary>
        public const string 禁用所有可執行程式碼 = "/safemode ";

        /// <summary>
        /// 載入一個指定的 ARX 或 DBX 應用程式。請使用以下格式:<路徑><檔名>.ARX,如果路徑或檔名中包含空格,應使用雙引號將路徑或檔名引起來。如果未提供任何路徑資訊,將使用程式搜尋路徑。/b 開關中介紹的相同的安全措施將應用於 ARX 檔案和 DBX 檔案。
        /// </summary>
        public const string ARX或DBX應用程式 = "/ld ";


        /// <summary>
        /// 獲取當前電腦上所有cad的exe檔案的路徑,LT不可用
        /// </summary>
        /// <returns></returns>
        /// 
        public static List<AcadProduct> GetCurPcInstallAcadInfor()
        {
            var rtn = new List<AcadProduct>();
            if (AcadHelper.AcadMainKey != null)
            {
                foreach (var item in AcadHelper.AcadMainKey.GetSubKeyNames())
                {
                    var ver = AcadHelper.AcadMainKey.OpenSubKey(item);
                    if (ver != null)
                    {
                        var pro = new AcadProduct(item);
                        if (pro.ExeDir != "") rtn.Add(pro);
                        ver.Close();
                    }
                }
            }
            return rtn;
        }

        /// <summary>
        /// 建立一個快捷方式
        /// </summary>
        /// <param name="lnkFilePath">快捷方式的完全限定路徑</param>
        /// <param name="targetFn">acad.exe的路徑</param>
        /// <param name="workDir"></param>
        /// <param name="description">描述</param>
        /// <param name="args">啟動引數</param>
        private static void CreateShortCut(string lnkFilePath, string targetFn, string workDir, string description, string args = "")
        {
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            dynamic shortcut = shell.CreateShortcut(lnkFilePath);
            shortcut.TargetPath = targetFn;
            shortcut.Arguments = args;
            shortcut.WorkingDirectory = workDir;
            shortcut.WindowStyle = 1;//設定執行方式,預設為常規視窗
            shortcut.Description = description;// '設定備註
            //shortcut.IconLocation = String.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//設定圖示路徑
            shortcut.Save();
        }


        /// <summary>
        ///  建立c#,vbnet,f# 等外掛的啟動引數,LT不可用
        /// </summary>
        /// <param name="dllfn"></param>
        /// <returns></returns>
        public static string CreateCadNetLoadDllStarUpArgs(string dllfn, string scrDir = "")
        {
            var scrfn = scrDir == "" ? Path.ChangeExtension(dllfn, ".scr") :
                Path.Combine(scrDir, Path.GetFileNameWithoutExtension(dllfn) + ".scr");
            using (StreamWriter sw = new StreamWriter(scrfn))
            {
                sw.WriteLine("secureload 0");
                sw.WriteLine("cmdecho 0");
                sw.WriteLine("netload " + (char)34 + dllfn + (char)34);
                sw.Close();
            }
            string rtn = 無產品徽標螢幕 + 指令碼名 + (char)34 + scrfn + (char)34;
            return rtn;
        }
        /// <summary>
        /// 建立dvb,arx等外掛的啟動引數,LT不可用
        /// </summary>
        /// <param name="Appfn">DVB/Arx檔名稱</param>
        /// <returns></returns>
        public static string CreateCadApploadStarUpArgs(string Appfn, string scrDir = "")
        {
            var scrfn = scrDir == "" ? Path.ChangeExtension(Appfn, ".scr") :
                Path.Combine(scrDir, Path.GetFileNameWithoutExtension(Appfn) + ".scr");
            var ext = Path.GetExtension(Appfn);
            string rtn;
            if (string.Compare(ext, ".arx") == 0)
            {
                rtn = 無產品徽標螢幕 + ARX或DBX應用程式 + (char)34 + scrfn + (char)34;
            }
            else
            {
                using (var sw = new StreamWriter(scrfn))
                {
                    sw.WriteLine("filedia 0");
                    sw.WriteLine("secureload 0");
                    sw.WriteLine("cmdecho 0");
                    //(command "_VBALOAD" "myproj.dvb")
                    //(vl-vbaload  "E:/Programs/Cut-Fill.dvb")
                    sw.WriteLine("(vl-vbaload " + (char)34 + Appfn.Replace("\\", "/") + (char)34 + ")");
                    sw.WriteLine("filedia 1");
                    sw.Close();
                }
                rtn = 無產品徽標螢幕 + 指令碼名 + (char)34 + scrfn + (char)34;
            }
            return rtn;
        }
        /// <summary>
        /// 建立cad外掛的臨時快捷啟動方式,LT不可用
        /// </summary>
        /// <param name="fn">外掛的檔名稱</param>
        /// <param name="cadexePath">cad的安裝目錄</param>
        /// <param name="desc">外掛描述</param>
        /// <param name="IsNetDll"></param>
        public static void CreateShortCutForApp(string fn, AcadProduct cadInfor, string desc, bool IsNetDll)
        {
            var tempDir = Environment.GetEnvironmentVariable("temp");
            var args = IsNetDll ? CreateCadNetLoadDllStarUpArgs(fn, tempDir) : CreateCadApploadStarUpArgs(fn, tempDir);
            var startMenuDir = $@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs";
            var lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Path.GetFileNameWithoutExtension(fn) + $"({cadInfor.AutoCADName}).lnk");
            var lnkPath1 = Path.Combine(startMenuDir, Path.GetFileNameWithoutExtension(fn) + $"({cadInfor.AutoCADName}).lnk");
            CreateShortCut(lnkPath, Path.Combine(cadInfor.ExeDir, "acad.exe"), Path.GetDirectoryName(fn), desc, args);
            CreateShortCut(lnkPath1, Path.Combine(cadInfor.ExeDir, "acad.exe"), Path.GetDirectoryName(fn), desc, args);
            //C:\ProgramData\Microsoft\Windows\Start Menu\Programs
        }

 
    }
檢視程式碼

 public class AcadProduct
    {
        public string RelaseVer { get; set; }
        public string Version { get; set; }
        public string ExeDir { get; set; }
        public string LastLaunchedLanguage { get; set; }
        public string CurVer { get; set; }
        public string AutoCADName { get; set; }
        //public string ArxRegsiter { get; set; }
        public AcadProduct(string acadRelaseName)
        {
            var curProKey = AcadHelper.AcadMainKey.OpenSubKey(acadRelaseName);
            this.RelaseVer = acadRelaseName;
            var curVer = curProKey.GetValue("CurVer", "").ToString();
            var languageVer = curProKey.GetValue("LastLaunchedLanguage", "").ToString();
            var curVer1 = curVer.Substring(0, curVer.IndexOf(":"));
            var acadPath = curProKey.OpenSubKey(curVer1 + "\\Install").GetValue("INSTALLDIR").ToString();
            if (Directory.Exists(acadPath))
            {
                this.AutoCADName = new DirectoryInfo(acadPath).Name;
                this.CurVer = curVer1;
                this.Version = curVer;
                this.ExeDir = acadPath;
                this.LastLaunchedLanguage = languageVer;

            }
            curProKey.Close();
        }

        public override string ToString()
        {
            return $"{AutoCADName} {LastLaunchedLanguage}";
        }

        public List<AcadArxAddin> GetAddins()
        {
            List<AcadArxAddin> rtns = new List<AcadArxAddin>();

            var curProKey = AcadHelper.AcadMainKey.OpenSubKey(this.RelaseVer);
            var rgs = curProKey.OpenSubKey(this.Version + "\\Applications");
            if (rgs != null)
            {
                foreach (var item in rgs.GetSubKeyNames())
                {
                    var addin = new AcadArxAddin(item, this);
                    rtns.Add(addin);
                }
                rgs.Close();
            }
            curProKey.Close();
            return rtns;
        }

    }

    public enum AddinType
    {
        Arx = 0,
        VBA = 1,
        Net = 2
    }

 

 AcadHelper.AcadMainKey這個屬性用下面的函式獲取

var autodeskKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Autodesk");
                var acadKey = autodeskKey?.OpenSubKey("AutoCAD");
                return acadKey;