是否可以使用C#程序关闭/休眠/重启/睡眠计算机?
答案 0 :(得分:3)
关闭/重启系统使用 -
using System.Management;
void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");
// **Flag 1 means we want to shut down the system. Use "2" to reboot**.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}
对于休眠和待机 - 如果您使用的是.NET 2.0,则可以直接调用
Application.SetSuspendState
。
答案 1 :(得分:1)
Process.Start("shutdown", "/s /t 0"); // shutdown
Process.Start("shutdown", "/r /t 0"); // shutdown and restart
Process.Start("shutdown", "/h /f"); // hibernate