110,533
社区成员
发帖
与我相关
我的任务
分享
/// 创建程序的快捷方式放入startup中
/// 即:D:\Documents and Settings\Administrator\「开始」菜单\程序\启动
/// 实现程序开机自启动
/// </summary>
public static void SetStartup()
{
try
{
//将要创建的快捷方式的完整路径(一定要加上.lnk)
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
+ "\\" + Application.ProductName + ".lnk";
//如果当前程序的快捷方式不存在,创建一个快捷方式保存在startup中
if (!System.IO.File.Exists(shortcutPath))
{
WshShell shell = new WshShell();
//创建快捷方式
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
//快捷方式的目标所在的位置(本程序的绝对路径)
shortcut.TargetPath = Application.ExecutablePath;
//指定应用程序的工作目录
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
shortcut.WindowStyle = 1;
//选中此快捷方式时显示的信息
shortcut.Description = "名称: "+ Application.ProductName+"\r\n公司: "+
Application.CompanyName +"\r\n版本: "+
Application.ProductVersion;
//指定快捷方式的图表的所在位置,它的值包含一个完整的路径和一个索引值
shortcut.IconLocation = Application.ExecutablePath + ",0";
shortcut.Save();
}
}
catch { }
}
/// <summary>
/// 把D:\Documents and Settings\Administrator\「开始」菜单\程序\启动
/// 中的本程序快捷方式删除
/// 实现程序不开机自启动
/// </summary>
public static void DeleteStartup()
{
try
{
//startup中当前程序的快捷方式的路径
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".lnk";
System.IO.File.Delete(shortcutPath);
}
catch { }
}