快捷方式實質上是一個擴展名爲 .LNK 的文件
方法如下:
首先要添加引用 (如圖)
就是那個Windows Script Host Object Model的類庫....
然後在程序中引入命名空間
有一些文件操作,所有要引入
關鍵方法如下:
/// 創建桌面快捷方式並開機啓動的方法
/// </summary>
private void ShortcutAndStartup()
{
//獲取當前系統用戶啓動目錄
string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
//獲取當前系統用戶桌面目錄
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
FileInfo fileStartup = new FileInfo(startupPath + "//億掌通.lnk");
FileInfo fileDesktop = new FileInfo(desktopPath + "//億掌通.lnk");
if (!fileDesktop.Exists)
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
"//" + "億掌通.lnk"
);
shortcut.TargetPath = Application.StartupPath + "//" + "Upgrade.exe";//啓動更新程序★
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
shortcut.WindowStyle = 1;
shortcut.Description = "億掌通";
shortcut.IconLocation = Application.ExecutablePath;
shortcut.Save();
}
if (!fileStartup.Exists)
{
//獲取可執行文件快捷方式的全部路徑
string exeDir = desktopPath + "//億掌通.lnk";
//把程序快捷方式複製到啓動目錄
System.IO.File.Copy(exeDir, startupPath + "//億掌通.lnk", true);
}
}