C# 建立捷徑 / 取捷徑目標

來源:互聯網
上載者:User
捷徑在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",                                               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;      }  }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.