我正在使用 System.Diagnostics.Process.Start("shutdown.exe", "-r -f -t 1")
来重启服务器。直到最近我注意到 server(win xp)
没有立即关闭时,它一直运行良好。我的日志显示应用程序在 shutdown.exe
命令后至少运行了 60 秒。
所以我开始怀疑是否有任何原因/条件/等导致 shutdown.exe
无法关闭系统,即使使用了 -f。
最佳答案
您可以 call the API directly 而不是调用可执行文件
[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);
如果出现问题,异常应该为您指明正确的方向。首先,您必须拥有 SeShutdownPrivilege
并且必须启用它。
考虑到权限后,完整代码应如下所示。注意:这假定使用 System.Security.AccessControl.Privelege
类,was released in an MSDN magazine article , 可供下载as linked from the article .
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);
这将执行与您尝试通过调用 shutdown.exe
执行的操作相同的功能,但最关键的是,如果存在任何阻止关机的故障或安全限制,将抛出异常成功。
关于c# - Shutdown.exe 没有立即关闭系统,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058586/