用C#创建快捷方式
前面的一个项目中,偶要用vs2003.net制作安装文件,需要为自己的程序创建快捷方式。那还不简单,用vs.net做过安装文件的朋友都知道,不用写一句代码,即可实现。但是,此法的弊端是:当你安装成功后,若安装文件夹中的文件发生变化(如:在最初安装时,安装文件夹下有 conf.xml ,然而,你的程序又将此文件删除拉),再点击快捷方式,看到的是提示你更新安装此软件。
若想程序不出现上面提示,而且能正常启动。偶介绍一法:不用上面方法创建快捷方式, 在安装时动态加载 dll 文件,让此 dll 在安装时调用执行。在该dll中执行创建快捷方式的操作。
前面的是废话,以下是创建快捷方式的示例,自己加吧,呵呵 ^ - ^
在您的项目中引入"Windows Script Host Object Model" COM library。
然后运行下面的代码:
1
using System;
2
using System.Runtime.InteropServices;
3
using IWshRuntimeLibrary;
4
5
namespace CreateShortcutCOM {
6
/// <summary>
7
/// This class creates a shortcut with COM interoperability
8
/// </summary>
9
class ShortcutDemo {
10
[STAThread]
11
static void Main(string[] args) {
12
// Get the app path and filename
13
string app = Environment.CurrentDirectory + @"\CreateShortcutCOM.exe";
14
15
try {
16
// Create a Windows Script Host Shell class
17
IWshShell_Class shell = new IWshShell_ClassClass();
18
// Define the shortcut file
19
IWshShortcut_Class shortcut = shell.CreateShortcut(app + ".lnk") as IWshShortcut_Class;
20
// Set all its properties
21
shortcut.Description = "Smart sample of creating shell shortcut";
22
shortcut.TargetPath = app;
23
shortcut.IconLocation = app + ",0";
24
// Save it
25
shortcut.Save();
26
}
27
catch(COMException ex) {
28
Console.WriteLine(ex.Message);
29
}
30
}
31
}
32
}
using System;2
using System.Runtime.InteropServices;3
using IWshRuntimeLibrary;4

5
namespace CreateShortcutCOM {6
/// <summary>7
/// This class creates a shortcut with COM interoperability8
/// </summary>9
class ShortcutDemo {10
[STAThread]11
static void Main(string[] args) {12
// Get the app path and filename13
string app = Environment.CurrentDirectory + @"\CreateShortcutCOM.exe";14

15
try {16
// Create a Windows Script Host Shell class17
IWshShell_Class shell = new IWshShell_ClassClass();18
// Define the shortcut file19
IWshShortcut_Class shortcut = shell.CreateShortcut(app + ".lnk") as IWshShortcut_Class;20
// Set all its properties21
shortcut.Description = "Smart sample of creating shell shortcut";22
shortcut.TargetPath = app;23
shortcut.IconLocation = app + ",0";24
// Save it25
shortcut.Save();26
} 27
catch(COMException ex) {28
Console.WriteLine(ex.Message);29
}30
}31
}32
}





浙公网安备 33010602011771号