日期:2014-05-17  浏览次数:21074 次

c#快捷方式
怎样用C#编写创建快捷方式呢?要注释。谢谢!!!!

------解决方案--------------------
using System;
 using System.Collections.Generic;
 using System.Text;
 using IWshRuntimeLibrary;
 //在引用里面添加“Windows Script Host ObjectModel”
 namespace Shortcuts
 {
public class Lnk
{
//"FolderPath"快捷方式存放的位置
//"PathLink"指向连接的文件
//"LnkName"快捷方式的文件
//"LnkNote"快捷方式的备注
//"IconLocationPath"指定快捷方式的图标
public void CreateShortcutLnk(string FolderPath, string PathLink,string LnkName, string LnkNote, string IconLocationPath)
{
try
{
WshShell shell = new WshShell();
IWshShortcut Shortcut =(IWshShortcut)shell.CreateShortcut(FolderPath + "\\" + LnkName +".lnk");
Shortcut.TargetPath = PathLink;
Shortcut.WindowStyle = 1;
Shortcut.Description =LnkNote;
Shortcut.IconLocation = IconLocationPath;
Shortcut.Save();
}
catch
{
throw new Exception("出错了");
}
}
}
}
 

上面的代码就是在C#中创建快捷方式的核心部分。其中这个类需要在引用中添加“WindowsScript Host Object Model”并且USING IWshRuntimeLibrary
下面是一个在VS.NET2005中制作的WINFORM程序。这个程序将演示如何在桌面上创建自己的快捷方式。
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 
 namespace 创建快捷方式
{
publicpartial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
 
private void button1_Click(object sender, EventArgs e)
{
Shortcuts.Lnk CreateLnk = new Shortcuts.Lnk();
CreateLnk.CreateShortcutLnk(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
Application.ExecutablePath, "我的应用程序", "我的应用程序",Application.ExecutablePath);
MessageBox.Show("成功创建快捷方式");
}
}
}