2015-05-13 181 views
0

有没有办法从服务中重新启动Windows(Server 2008,Server 2012)?我试过了:从服务中重新启动Windows

System.Diagnostics.Process.Start("cmd.exe /c shutdown -f -r -t 0") 

无济于事。我在这里看到的解决方案:

How to shut down the computer from C#

http://www.stackoverflow.com/questions/1215139/reboot-machine-from-a-c-wpf-app

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/shut-down-restart-log-off-and-forced-log-off-system-using-C-Sharp/

,机器只是不希望重新启动。

当我从命令行运行命令时,它工作。从服务中运行时

cmd.exe /c shutdown -f -r -t 0 

甚至

shutdown -f -r -t 0 

什么也没有发生。我甚至修改它来运行:

c:\\windows\\system32\\cmd.exe /c c:\\windows\\system32\\shutdown.exe -f -r -t 0 

而同样的结果,没有任何反应。再次从命令行运行时,它会正确重启。

+1

我编辑了自己的冠军。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

+1

http://stackoverflow.com/questions/22606426/process-start-does-not-work-when-called-from-windows-service –

+0

当您从服务中执行此操作时会发生什么? – CathalMF

回答

1

我会怀疑,因为窗口服务没有GUI ...它不能运行命令提示符。

寻找到一个Win32 API的解决方案......像ExitWindowsEx()或InitiateSystemShutdown或关机

+0

谢谢Marc,你有代码示例或链接分享? – joelc

+0

试过这个,没有工作。 HTTP:// debugmode。net/2010/05/18/shutdown-restart-log-off-and-forced-log-off-system-using-c/ – joelc

+0

使用此处解决方案中找到的令牌调节器后工作。 http://stackoverflow.com/questions/24726116/when-using-exitwindowsex-logoff-works-but-shutdown-and-restart-do-not – joelc

1

试试这个..

Process myPro = new Process() 
myPro.StartInfo.FileName = "cmd.exe"; 
myPro.StartInfo.Arguments = “/c shutdown –f –r –t 0”; 
myPro.StartInfo.UseShellExecute = false; 
myPro.CreateNoWindow = true; 
myPro.Start(); 

而且其中你的cmd.exe文件?如果它没有跑出你的应用程序运行的同一目录?您可能需要提供指向cmd.exe文件的路径。

例子..

myPro.StartInfo.FileName = "c:\desktop\myStuff\cmd.exe"; 

希望它可以帮助

+0

谢谢Marc,除非它作为一项服务运行,并且不能运行命令提示符,否则这将起作用。根据上面的问题,我也尝试设置显式路径。下面的Marc Johnston的解决方案是正确的,尽管您可能适合可以产生命令提示符的应用程序。谢谢! – joelc

+0

haxor没问题 –