This post was last edited by cpr1993 on 2019-1-9 11:02
First, use the using statement to add the namespace we need:using System.Diagnostics; using System.Runtime.InteropServices;
Shutdown Process.Start("shutdown","/s /t 0"); The parameter /s means to shut down the computerThe parameter /t 0 means to tell the computer to execute the command after 0 seconds
Restart Process.Start("shutdown", "/r /t 0"); The parameter /r means to restart the computer
Cancel
You need to declare a Windows API function in your class using DllImport: [DllImport("user32")] public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
Then, you can use the following code to log out:
ExitWindowsEx(0,0);
LockLike logout, you need to declare a function: [DllImport("user32")] public static extern void LockWorkStation();
Then, you can use the following code to achieve locking:
LockWorkStation();
Dormancy and sleep
Again, you still need to declare a function: [DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);
Implement hibernation with the following code: SetSuspendState(true, true, true);
To achieve sleep, the code is as follows: SetSuspendState(false, true, true);
|