快捷方式实质上是一个扩展名为 .LNK 的文件
方法如下:
首先要添加引用 (如图)

就是那个Windows Script Host Object Model的类库....
然后在程序中引入命名空间
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->usingIWshRuntimeLibrary;
有一些文件操作,所有要引入
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->usingSystem.IO;
关键方法如下:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->///<summary>
///创建桌面快捷方式并开机启动的方法
///</summary>
privatevoidShortcutAndStartup()
{
//获取当前系统用户启动目录
stringstartupPath=Environment.GetFolderPath(Environment.SpecialFolder.Startup);
//获取当前系统用户桌面目录
stringdesktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
FileInfofileStartup=newFileInfo(startupPath+"//亿掌通.lnk");
FileInfofileDesktop=newFileInfo(desktopPath+"//亿掌通.lnk");
if(!fileDesktop.Exists)
{
WshShellshell=newWshShell();
IWshShortcutshortcut=(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)
{
//获取可执行文件快捷方式的全部路径
stringexeDir=desktopPath+"//亿掌通.lnk";
//把程序快捷方式复制到启动目录
System.IO.File.Copy(exeDir,startupPath+"//亿掌通.lnk",true);
}
}
///创建桌面快捷方式并开机启动的方法
///</summary>
privatevoidShortcutAndStartup()
{
//获取当前系统用户启动目录
stringstartupPath=Environment.GetFolderPath(Environment.SpecialFolder.Startup);
//获取当前系统用户桌面目录
stringdesktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
FileInfofileStartup=newFileInfo(startupPath+"//亿掌通.lnk");
FileInfofileDesktop=newFileInfo(desktopPath+"//亿掌通.lnk");
if(!fileDesktop.Exists)
{
WshShellshell=newWshShell();
IWshShortcutshortcut=(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)
{
//获取可执行文件快捷方式的全部路径
stringexeDir=desktopPath+"//亿掌通.lnk";
//把程序快捷方式复制到启动目录
System.IO.File.Copy(exeDir,startupPath+"//亿掌通.lnk",true);
}
}
本文介绍了一种使用C#创建桌面快捷方式及确保应用程序随系统启动的方法。通过调用WindowsScriptHostObjectModel类库,可以实现为指定的应用程序创建快捷方式,并将其放置于用户的桌面及启动文件夹中。
234

被折叠的 条评论
为什么被折叠?



