This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15270|Reply: 1

[Tips] How does C# code make your computer shut down, restart, log out, lock, shut down...

[Copy link]
Posted on 1/9/2019 10:52:02 AM | | |
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);










Previous:Elasticsearch:No handler for type [string] declared on field[XX]的解决办法
Next:Read/generate csv files
Posted on 1/9/2019 10:53:17 AM |
The content of the post is messed up, edit it
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com