Possible Duplicate:
How to shutdown the computer from C#
I want to bring up a restart computer message box after an event occurs in my Win Form application. What command can I use to restart the computer if the user chooses Yes?
Possible Duplicate:
How to shutdown the computer from C#
I want to bring up a restart computer message box after an event occurs in my Win Form application. What command can I use to restart the computer if the user chooses Yes?
如前所述,您还需要调用AdjustTokenPrivileges
,因为SE_SHUTDOWN_NAME
默认情况下是不活动的。
MSDN 上的一大堆信息在这里
你应该使用 WINAPI 。以下功能可以为您完成电源任务:
BOOL WINAPI ExitWindowsEx(
_In_ UINT uFlags,
_In_ DWORD dwReason
);
请注意,此函数位于 user32.dll 中。只需重新启动:
ExitWindowsEx(2,4);
这是标志的完整列表:链接到 msdn
现在这里是示例 C# 代码:
using System.Runtime.InteropServices;
[DllImport("user32.dll", SetLastError=true)]
public static extern bool ExitWindowsEx(uint uFlags,uint dWReason);
public static void Main()
{
ExitWindowsEx(2,4);
}
Process.Start("shutdown","/r /t 0");
这个特殊的解决方案对我有用:https ://stackoverflow.com/a/102583/1505128
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);
}
}