//调用方法:设置开机启动
SetAutoRun(Process.GetCurrentProcess().ProcessName, true, Application.StartupPath + @"\" + Process.GetCurrentProcess().ProcessName + ".exe");
/// <summary>
/// 设置为开机启动
/// </summary>
private void SetAutoRun(string keyName, bool isAutoRun, string filePath = "")
{
RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (runKey == null)
runKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (isAutoRun)
runKey.SetValue(keyName, filePath);
else
runKey.DeleteValue(keyName, false);
runKey.Close();
}