C#程序开机运行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 系统启动项 /// <summary> /// 获取启动项值 /// </summary> /// <param name="name">启动项名称</param> /// <returns></returns> public static string GetRunItemValue(string name) { RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); string strAutoRun = run.GetValue("QWU8_MESI_AutoRun").ToStr(); loca.Close(); return strAutoRun; } /// <summary> /// 添加系统启动项 /// </summary> /// <param name="name">启动项名称</param> /// <returns>返回失败信息</returns> public static string AddRunItem(string name) { return AddRunItem(name, Application.ExecutablePath); } /// <summary> /// 添加系统启动项 /// </summary> /// <param name="name">启动项名称</param> /// <param name="path">启动项全路径</param> /// <returns>返回失败信息</returns> public static string AddRunItem(string name, string path) { string errorMsg = string.Empty; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { if (GetRunItemValue(name).Length > 0) run.DeleteValue(name); run.SetValue(name, path); run.Close(); loca.Close(); } catch (Exception ex) { errorMsg = ex.Message; run.Close(); loca.Close(); } finally { } return errorMsg; } /// <summary> /// 删除系统启动项 /// </summary> /// <param name="name">启动项名称</param> /// <returns>返回失败信息</returns> public static string DeleteRunItem(string name) { string errorMsg = string.Empty; RegistryKey loca = Registry.LocalMachine; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { if (GetRunItemValue(name).Length >0) run.DeleteValue(name); run.Close(); loca.Close(); } catch (Exception ex) { errorMsg = ex.Message; run.Close(); loca.Close(); } return errorMsg; } #endregion