C# 实现程序开机自启动
private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
try
{
//设置开机自启动
if (checkBox8.Checked == true)
{
/*方法一*/
string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
//获得文件的当前路径
string dir = Directory.GetCurrentDirectory();
//获取可执行文件的全部路径
string exeDir = dir + @"\自动备份.exe.lnk";
File.Copy(exeDir, StartupPath + @"\自动备份.exe.lnk", true);
/*方法二*/
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("JcShutdown", path);
rk2.Close();
rk.Close();
}
//取消开机自启动
else
{
/*方法一*/
string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
System.IO.File.Delete(StartupPath + @"\EcgNetPlug.exe.lnk");
/*方法二*/
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("JcShutdown", false);
rk2.Close();
rk.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}