c#
void 开机启动()
{
string 程序名 = Application.ExecutablePath,
名称 = 程序名.Substring(程序名.LastIndexOf("\\") + 1);
名称 = 名称.Remove(名称.LastIndexOf("."));
if (!System.IO.File.Exists(程序名)) return;
string 快捷文件 = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + 名称 + ".lnk";
if (System.IO.File.Exists(快捷文件))
{
if (MessageBox.Show("确定要删除 [" + 名称 + "] 开机启动吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
System.IO.File.Delete(快捷文件);
}
else
{
WshShell 外壳 = new WshShell();
IWshShortcut 快捷键 = (IWshShortcut)外壳.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + 名称 + ".lnk");
快捷键.TargetPath = 程序名;
快捷键.WorkingDirectory = System.Environment.CurrentDirectory;
快捷键.WindowStyle = 1;
快捷键.Description = 名称;
快捷键.IconLocation = 程序名;
快捷键.Save();
MessageBox.Show("[" + 名称 + "] 开机启动,设置完成!");
}
}
Microsoft.Win32.RegistryKey 注册项 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
//Microsoft.Win32.RegistryKey 注册项 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
//if (注册项 == null || !注册项.GetValueNames().Contains(名称))
// 注册项.SetValue(名称, "\"" + 程序名 + "\" -autorun");/*设置开机启动WIN7/64带参数和带双引号*/
//else
// if (注册项.ValueCount > 1)
// 注册项.DeleteValue(名称);/*取消开机启动*/
//注册项.Close();
//string 程序路径 = System.Reflection.Assembly.GetExecutingAssembly().Location;
//RegistryKey src = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true).OpenSubKey("Windows", true).OpenSubKey("CurrentVersion", true).OpenSubKey("Run", true);