using Microsoft.Win32;//添加命名空间
原文地址:http://wenda.so.com/q/1375246108065386
/// <summary> /// 设置开机自启动__写入注册表 /// </summary> /// <returns></returns> public static bool SetSelfStarting() { try { string exeDir = Application.ExecutablePath;//要启动的程序绝对路径 RegistryKey rk = Registry.LocalMachine; RegistryKey softWare = rk.OpenSubKey("SOFTWARE"); RegistryKey microsoft = softWare.OpenSubKey("Microsoft"); RegistryKey windows = microsoft.OpenSubKey("Windows"); RegistryKey current = windows.OpenSubKey("CurrentVersion"); RegistryKey run = current.OpenSubKey(@"Run", true);//这里必须加true就是得到写入权限 run.SetValue("自己起个名字", exeDir); return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } } /// <summary> /// 取消开机自启__删除注册表 /// </summary> /// <returns></returns> public static bool CancelSelfStarting() { try { RegistryKey rk = Registry.LocalMachine; RegistryKey softWare = rk.OpenSubKey("Software"); RegistryKey microsoft = softWare.OpenSubKey("Microsoft"); RegistryKey windows = microsoft.OpenSubKey("Windows"); RegistryKey current = windows.OpenSubKey("CurrentVersion"); RegistryKey run = current.OpenSubKey(@"Run", true); run.DeleteValue("自己起的名字");//删除myPro的值 return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } }
原文地址:http://www.cnblogs.com/steed-zgf/archive/2012/04/26/2471652.html
【设置开机自启动】
using Microsoft.Win32;//添加命名空间 public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true); runKey.SetValue(keyName,filePath); runKey.Close(); } catch { return false; } return true; }
【取消开机自启动】
using Microsoft.Win32;//添加命名空间 public static bool DeleteAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true); runKey.DeleteValue(keyName,filePath); runKey.Close(); } catch { return false; } return true; }