我在WinForms应用程序使用此命令来关闭我的电脑取消PC关机:使用shutdown.exe的CMD C#
System.Diagnostics.Process.Start("shutdown", "/s");
此时Windows 8和8.1显示一条消息,告诉我,我的PC将在1分钟内关闭。没有选择取消。
我该怎么办(在1分钟内)发送一条命令到cmd/shutdown.exe至取消关闭PC?
我在WinForms应用程序使用此命令来关闭我的电脑取消PC关机:使用shutdown.exe的CMD C#
System.Diagnostics.Process.Start("shutdown", "/s");
此时Windows 8和8.1显示一条消息,告诉我,我的PC将在1分钟内关闭。没有选择取消。
我该怎么办(在1分钟内)发送一条命令到cmd/shutdown.exe至取消关闭PC?
尝试关机/ A
的/如果它在时间限制内发送指令应中止关机。
退房的Technet对关闭文档:http://technet.microsoft.com/en-us/library/bb491003.aspx
你可以使用的PInvoke取消关机(调用user32.dll
CancelShutdown
),而不是调用其他程序。
请参见:http://www.pinvoke.net/default.aspx/user32/CancelShutdown.html
这相当于什么shutdown /a
在做什么。
您可以通过P/Invokes启动和中止系统关闭至advapi32
。见InitiateSystemShutdownEx
和AbortSystemShutdown
。启动和取消系统关闭都需要SeShutdownPrivilege
关闭本地计算机,或SeRemoteShutdownPrivilege
要通过网络关闭计算机。
当考虑到特权时,完整的代码应如下所示。注意:这里假定使用System.Security.AccessControl.Privelege
类,其中was released in an MSDN magazine article,可供下载as linked from the article。
[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);
}