C#实现开机自动启动设置代码分享
程序员文章站
2023-12-09 19:45:27
///
/// 设置程序开机启动
/// 或取消开机启动
///
///...
/// <summary> /// 设置程序开机启动 /// 或取消开机启动 /// </summary> /// <param name="started">设置开机启动,或者取消开机启动</param> /// <param name="exename">注册表中程序的名字</param> /// <param name="path">开机启动的程序路径</param> /// <returns>开启或则停用是否成功</returns> public static bool runwhenstart(bool started, string exename, string path) { registrykey key = registry.localmachine.opensubkey("software\\microsoft\\windows\\currentversion\\run", true);//打开注册表子项 if (key == null)//如果该项不存在的话,则创建该子项 { key = registry.localmachine.createsubkey("software\\microsoft\\windows\\currentversion\\run"); } if (started == true) { try { key.setvalue(exename, path);//设置为开机启动 key.close(); } catch { return false; } } else { try { key.deletevalue(exename);//取消开机启动 key.close(); } catch { return false; } } return true; }