public class ControlComputer { /// <summary> /// 重启当前程序 /// </summary> public static void ReStartApplicationSelf() { System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location); } /// <summary> /// 重启电脑 /// </summary> public static void ReStartComputer() { // 参数 /r 的意思是要重启计算机 Process.Start("shutdown", "/r /t 0"); } /// <summary> /// 关机 /// </summary> public static void ColseDownComputer() { // 参数 /s 的意思是要关闭计算机 // 参数 /t 0 的意思是告诉计算机 0 秒之后执行命令 Process.Start("shutdown", "/s /t 0"); } [DllImport("user32")] public static extern bool ExitWindowsEx(uint uFlags, uint dwReason); /// <summary> /// 注销 /// </summary> public static void ExitWindows() { ExitWindowsEx(0, 0); } [DllImport("user32")] public static extern void LockWorkStation(); /// <summary> /// 锁定 /// </summary> public static void LockWindows() { LockWorkStation(); } [DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent); /// <summary> /// 睡眠 /// </summary> public static void SleepWindows() { SetSuspendState(false, true, true); } /// <summary> /// 休眠 /// </summary> public static void dormancyWindows() { SetSuspendState(true, true, true); } }
/// <summary> /// 程序自启动 /// </summary> /// <param name="exeName">应用程序名称</param> /// <param name="exePath">文件路径</param> public static void autoRun(string exeName, string exePath) { RegistryKey runItem = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); try { if (runItem != null) { runItem.SetValue(exeName, exePath); } } catch (Exception e) { throw e; } } /// <summary> /// 删除自启动 /// </summary> /// <param name="exeName">文件名称</param> public static void deleteAutoRun(string exeName) { RegistryKey runItem = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); try { if (runItem != null) { runItem.DeleteSubKey(exeName); } } catch (Exception e) { throw e; } }
原文:https://www.cnblogs.com/Zingu/p/14713735.html