1 /// <summary> 2 /// 写进注册表,以便开机启动 3 /// </summary> 4 private void SetAutoStar() 5 { 6 try 7 { 8 string filepath = Assembly.GetExecutingAssembly().Location; 9 string runName = Path.GetFileNameWithoutExtension(filepath); 10 11 RegistryKey hkml = Registry.LocalMachine; 12 RegistryKey runKey = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); 13 runKey.SetValue(runName, filepath); 14 runKey.Close(); 15 } 16 catch (Exception ex) 17 { 18 MessageBox.Show(ex.Message); 19 } 20 21 }
取消自动重启,删除相应的注册表
1 /// <summary> 2 /// 取消开机启动,删除注册表中的值 3 /// </summary> 4 public void CancleAutoRun() 5 { 6 try 7 { 8 string filepath = Assembly.GetExecutingAssembly().Location; 9 string runName = Path.GetFileNameWithoutExtension(filepath); 10 RegistryKey hkml = Registry.LocalMachine; 11 RegistryKey runKeys = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); 12 runKeys.DeleteValue(runName); 13 } 14 catch 15 { } 16 }