C#創(chuàng)建快捷方式簡單描述
C#語言還是比較常見的東西,這里我們主要介紹C#創(chuàng)建快捷方式,包括介紹使用 WSH 創(chuàng)建快捷方式等方面。
C#創(chuàng)建快捷方式對于絕大多數(shù) Windows 用戶來說都是小菜一碟了,然而,這項工作卻為程序員帶來不少麻煩。.net 沒有提供簡便直接的創(chuàng)建快捷方式的方法,那么在 .NET 中我們?nèi)绾螢閼?yīng)用程序創(chuàng)建快捷方式呢?
1. C#創(chuàng)建快捷方式
C#創(chuàng)建快捷方式實質(zhì)上是一個擴展名為 .LNK 的文件。右擊一個快捷方式文件并選擇屬性,跳到快捷方式選項卡:
你可以看到一個快捷方式包含如下數(shù)據(jù):
◆快捷方式的名字
◆快捷方式所指向的目標(biāo)所在的位置
◆快捷方式所指向的目標(biāo)的工作目錄
◆激活該快捷方式的熱鍵
◆快捷方式所指向的目標(biāo)運行時的窗口風(fēng)格(普通、最大化和最小化)
◆該快捷方式的描述性文字
◆快捷方式的圖標(biāo)所在的位置
2. 使用 WSH 創(chuàng)建快捷方式
2.1 添加 WSH 的引用
這里我使用 Visual C# 2005 Express Edition Beta 2 來開發(fā)的,添加引用的方法非常簡單,右擊你的項目并選擇添加引用,選擇 COM 選項卡并選擇 Windows Script Host Object Model:
2.2 創(chuàng)建你的快捷方式
創(chuàng)建一個快捷方式的完整代碼如下:
- using System;
- using IWshRuntimeLibrary;
- class Program
- {
- static void Main(string[] args)
- {
- WshShell shell = new WshShell();
- IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
- Environment.GetFoldERPath(Environment.SpecialFolder.DesktopDirectory) +
- "\\" + "Allen’s Application.lnk"
- );
- shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
- shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
- shortcut.WindowStyle = 1;
- shortcut.Description = "Launch Allen’s Application";
- shortcut.IconLocation = System.Environment.
SystemDirectory + "\\" + "shell32.dll, 165";- shortcut.Save();
- }
- }
【編輯推薦】