Welcome

首页 / 软件开发 / C# / C#创建快捷方式方法简介

C#创建快捷方式方法简介2011-05-24本文主要介绍C#创建快捷方式,包括介绍使 用 WSH 创建快捷方式等方面。

C#创建快捷方式对于绝大多数 Windows 用户来说都是小菜一碟了,然而,这 项工作却为程序员带来不少麻烦。.net 没有提供简便直接的创建快捷方式的方法 ,那么在 .NET 中我们如何为应用程序创建快捷方式呢?

1. C#创建快捷方式

C#创建快捷方式实质上是一个扩展名为 .LNK 的文件。右击一个快捷方式文件 并选择属性,跳到快捷方式选项卡:

你可以看到一个快捷方式包含如下数据:

◆快捷方式的名字

◆快捷方式所指向的目标所在的位置

◆快捷方式所指向的目标的工作目录

◆激活该快捷方式的热键

◆快捷方式所指向的目标运行时的窗口风格(普通、最大化和最小化)

◆该快捷方式的描述性文字

◆快捷方式的图标所在的位置

2. 使用 WSH 创建快捷方式

2.1 添加 WSH 的引用

这里我使用 Visual C# 2005 Express Edition Beta 2 来开发的,添加引用 的方法非常简单,右击你的项目并选择添加引用,选择 COM 选项卡并选择 Windows Script Host Object Model:

2.2 创建你的快捷方式

创建一个快捷方式的完整代码如下:

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();
}
}