C# 創建快捷方式 / 取快捷方式目標

ny8p 9年前發布 | 2K 次閱讀 C#

快捷方式在Win32上應用的非常之多,比如某個軟件安裝完畢后會創建一些快捷方式

到特定目錄下,那么在.NET上我并未發現有什么托管類可以操作快捷方式,那么我們

又必須要使用它 為此我預先寫了一份快捷方式應用的代碼,希望對大家有益健康咯。

    using System;  
    using System.IO;  
    using System.Runtime.InteropServices;  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            CreateShortCut( // 創建快捷方式  
                    @"C:\Users\windo\Desktop\ican.lnk",  
                    @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE",  
                    @"http://blog.csdn.net/u012395622",  
                    @"遠去的山河 沉寂  戀過的風景 如昔",  
                   AppDomain.CurrentDomain.BaseDirectory,  
                   @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE, 0",  
                   "CTRL+ALT+Z"  
                );  

        }  

        public static readonly Guid CLSID_WshShell = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");  
        public static string GetShortCutTarget(string lnk) // 取快捷方式目標  
        {  
            if (lnk != null && File.Exists(lnk))  
            {  
                dynamic objWshShell = null, objShortcut = null;  
                try  
                {  
                    objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));  
                    objShortcut = objWshShell.CreateShortcut(lnk);  
                    return objShortcut.TargetPath;  
                }  
                finally  
                {  
                    Marshal.ReleaseComObject(objShortcut);  
                    Marshal.ReleaseComObject(objWshShell);  
                }  
            }  
            return string.Empty;  
        }  

        public static bool CreateShortCut(string lnkFileName,  
                string targetPath,  
                string arguments,  
                string remark,  
                string workingDirectory,  
                string iconLocation,  
                string hotKey  
            )  
        {  
            if (lnkFileName != null && lnkFileName.Length > 0)  
            {  
                dynamic objWshShell = null, objShortcut = null;  
                try  
                {  
                    objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));  
                    objShortcut = objWshShell.CreateShortcut(lnkFileName);  

                    objShortcut.WindowStyle = 1;  

                    objShortcut.Hotkey = hotKey; // 熱鍵  
                    objShortcut.TargetPath = targetPath; // 目標文件  
                    objShortcut.Arguments = arguments; // 參數  
                    objShortcut.Description = remark; // 備注  
                    objShortcut.WorkingDirectory = workingDirectory; // 起始位置  
                    objShortcut.IconLocation = iconLocation; // 圖標位置  

                    objShortcut.Save();  

                    return true;  
                }  
                finally  
                {  
                    Marshal.ReleaseComObject(objShortcut);  
                    Marshal.ReleaseComObject(objWshShell);  
                }  
            }  
            return false;  
        }  
    }  

 本文由用戶 ny8p 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!