C# 使用ShutDown.exe cmd C取消电脑关机
我正在WinForms应用程序中使用此命令关闭我的电脑: System.Diagnostics.Process.Startshutdown,/s 此时,Windows 8和8.1会显示一条消息,告诉我我的电脑将在1分钟后关机。没有取消的选项 在这1分钟内,我如何向cmd/shutdown.exe发送命令以取消关闭电脑?尝试关闭/a 如果在时间限制内发送/a命令,则该命令应中止关机 查看Technet关于关机的文档:您可以使用pinvoke在user32.dll中调用CancelShutdown来取消关机,而不是调用另一个进程 见:C# 使用ShutDown.exe cmd C取消电脑关机,c#,.net,winforms,C#,.net,Winforms,我正在WinForms应用程序中使用此命令关闭我的电脑: System.Diagnostics.Process.Startshutdown,/s 此时,Windows 8和8.1会显示一条消息,告诉我我的电脑将在1分钟后关机。没有取消的选项 在这1分钟内,我如何向cmd/shutdown.exe发送命令以取消关闭电脑?尝试关闭/a 如果在时间限制内发送/a命令,则该命令应中止关机 查看Technet关于关机的文档:您可以使用pinvoke在user32.dll中调用CancelShutdown来
这相当于关机/a正在执行的操作。您可以通过调用advapi32来启动和中止系统关机。见和。启动和取消系统关闭都需要SeShutdownPrivilege在本地关闭计算机,或SeRemoteShutdownPrivilege通过网络关闭计算机 当考虑特权时,完整的代码应该如下所示。注意:这假设使用System.Security.AccessControl.Privilege类,该类可供下载
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool InitiateSystemShutdownEx(
string lpMachineName,
string lpMessage,
uint dwTimeout,
bool bForceAppsClosed,
bool bRebootAfterShutdown,
uint dwReason);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool AbortSystemShutdown(string lpMachineName);
public static void Shutdown()
{
Privilege.RunWithPrivilege(Privilege.Shutdown, true, (_) =>
{
if (!NativeMethods.InitiateSystemShutdownEx(null /* this computer */,
"My application really needs to restart",
30 /* seconds */, true /* force shutdown */,
true /* restart */, 0x4001 /* application: unplanned maintenance */))
{
throw new Win32Exception();
}
}, null);
}
public static void CancelShutdown()
{
Privilege.RunWithPrivilege(Privilege.Shutdown, true, (_) =>
{
if (!NativeMethods.AbortSystemShutdown(null /* this computer */))
{
throw new Win32Exception();
}
}, null);
}