C#关机、注销、待机、重启操作示例

C#控制关机、重启、注销、待机等操作,其实蛮,你只需要调用一些 Windows 的 API 或者用Process.Start()就能轻松搞定。比如,关机就可以用shutdown.exe命令:Process.Start("shutdown", "/s /t 0");,重启的话,只需要换个参数就好。想要更细致控制,比如待机、休眠,C#也能通过调用系统的powrprof.dll来实现,像这样:

[DllImport("powrprof.dll")]
private static extern bool SetSuspendState(bool hiberate, bool force, bool disableWakeEvent);
public static void Suspend() {
  SetSuspendState(false, false);
}
。不过要注意,这些操作一般都需要管理员权限,所以最好检查下权限。对于新手来说,源码中的注释重要,你理解每个步骤是怎么工作的。

如果你正打算做个自动化任务,或者要控制计算机状态,这个 C#源码就是一个不错的参考。用得好,能帮你搞定不少工作哦。

rar
.rar 预估大小:23个文件
folder
系统关机系列 文件夹
file
系统关机系列.sln 944B
file
系统关机系列.suo 14KB
folder
系统关机系列 文件夹
file
Form1.cs 1KB
folder
bin 文件夹
folder
Debug 文件夹
file
系统关机系列.vshost.exe 14KB
file
系统关机系列.vshost.exe.manifest 490B
file
系统关机系列.exe 13KB
file
系统关机系列.pdb 26KB
folder
obj 文件夹
folder
Debug 文件夹
file
系统关机系列.csproj.FileListAbsolute.txt 745B
file
系统关机系列.csproj.GenerateResource.Cache 847B
file
系统关机系列.exe 13KB
file
系统关机系列.Form1.resources 180B
file
系统关机系列.pdb 26KB
folder
TempPE 文件夹
file
系统关机系列.Properties.Resources.resources 180B
folder
Properties 文件夹
file
Resources.Designer.cs 3KB
file
Settings.settings 249B
file
Resources.resx 5KB
file
Settings.Designer.cs 1KB
file
AssemblyInfo.cs 1KB
file
Program.cs 499B
file
系统关机系列.csproj 4KB
file
Form1.Designer.cs 5KB
file
close.cs 23KB
file
Form1.resx 6KB
rar 文件大小:47.09KB