2015-05-13 197 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沒問題 –