C# 设置开机自动启动方法
我们做系统时,有时候需要开机启动,下面就是开机启动方法:
/// <summary> /// 设置开机启动 /// </summary> /// <param name="started">是否开机启动</param> /// <param name="exeName">程序名称</param> /// <param name="path">程序路径</param> /// <returns></returns> public static bool SetAutoStart(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; }
直接调用就可以了,会在注册表写入开机启动