`
lovnet
  • 浏览: 6704498 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

(转)Winform 创建桌面快捷方式并开机启动

阅读更多

快捷方式实质上是一个扩展名为 .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);
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics