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

本文介绍了一种使用C#创建桌面快捷方式及确保应用程序随系统启动的方法。通过调用WindowsScriptHostObjectModel类库,可以实现为指定的应用程序创建快捷方式,并将其放置于用户的桌面及启动文件夹中。

快捷方式实质上是一个扩展名为 .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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值