我在一个WinForms应用程序中使用这个命令来关闭我的PC:
System.Diagnostics.Process.Start("shutdown", "/s");
此时,Windows 8和8.1将显示一条消息,告诉我我的PC将在1分钟内关机。没有取消的选择。
我如何才能(在那1分钟内)向cmd/快门. the发送命令以取消关闭PC
发布于 2014-09-21 17:23:07
试着关机/a
如果/a命令是在时限内发送的,则它应该中止关机。
查看Technet的关机文档:http://technet.microsoft.com/en-us/library/bb491003.aspx
发布于 2014-09-21 18:11:40
您可以通过对advapi32
的P/调用来启动和中止系统关闭。见InitiateSystemShutdownEx
和AbortSystemShutdown
。启动和取消系统关闭都需要SeShutdownPrivilege
在本地关闭计算机,或者SeRemoteShutdownPrivilege
通过网络关闭计算机。
当考虑到特权时,完整的代码应该如下所示。注意:这假设使用System.Security.AccessControl.Privelege
类,发表于MSDN杂志的一篇文章可以下载从文章中链接到。
[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);
}
https://stackoverflow.com/questions/25961826
复制相似问题