首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >取消PC ShutDown使用shutdown.exe cmd C#

取消PC ShutDown使用shutdown.exe cmd C#
EN

Stack Overflow用户
提问于 2014-09-21 17:18:50
回答 2查看 724关注 0票数 0

我在一个WinForms应用程序中使用这个命令来关闭我的PC:

System.Diagnostics.Process.Start("shutdown", "/s");

此时,Windows 8和8.1将显示一条消息,告诉我我的PC将在1分钟内关机。没有取消的选择。

我如何才能(在那1分钟内)向cmd/快门. the发送命令以取消关闭PC

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-21 17:23:07

试着关机/a

如果/a命令是在时限内发送的,则它应该中止关机。

查看Technet的关机文档:http://technet.microsoft.com/en-us/library/bb491003.aspx

票数 7
EN

Stack Overflow用户

发布于 2014-09-21 18:11:40

您可以通过对advapi32的P/调用来启动和中止系统关闭。见InitiateSystemShutdownExAbortSystemShutdown。启动和取消系统关闭都需要SeShutdownPrivilege在本地关闭计算机,或者SeRemoteShutdownPrivilege通过网络关闭计算机。

当考虑到特权时,完整的代码应该如下所示。注意:这假设使用System.Security.AccessControl.Privelege类,发表于MSDN杂志的一篇文章可以下载从文章中链接到

代码语言:javascript
运行
复制
[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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25961826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档