0

Is there a way to reboot Windows from within a service (Server 2008, Server 2012)? I've tried:

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

To no avail. I've looked at solutions here:

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/

and the machine just doesn't want to reboot.

When I run the command from the command line, it works.

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

or even

shutdown -f -r -t 0

Nothing happens when run from within the service. I even modified it to run:

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

And same result, nothing happens. Again when I run from the command line, it reboots properly.

Community
  • 1
  • 1
joelc
  • 2,687
  • 5
  • 40
  • 60

2 Answers2

1

I would suspect that because a window service has no GUI ... that it cannot run a command prompt.

Look into a win32 API solution ... like ExitWindowsEx() or InitiateSystemShutdown or shutdown

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
  • Thanks Marc, do you have a code example or link to share? – joelc May 13 '15 at 16:39
  • Tried this one, didn't work. http://debugmode.net/2010/05/18/shut-down-restart-log-off-and-forced-log-off-system-using-c/ – joelc May 13 '15 at 16:44
  • Worked after using the token adjuster found in the solution here. http://stackoverflow.com/questions/24726116/when-using-exitwindowsex-logoff-works-but-shutdown-and-restart-do-not – joelc May 13 '15 at 16:48
  • No sweat Marc, I found a code sample in the link in my previous comment. I marked yours as the answer. Have a great day! – joelc May 13 '15 at 17:40
  • I don't think it's GUI related. `shutdown.exe` can be run directly via `CreateProcess` as a `DETACHED_PROCESS` (no console window), which works for me on a remote shell running in session 0 (Windows 7 service). I don't know why cmd.exe even factors into this. If you run `shutdown.exe` as a non-detached process the system will create a console window for it that has nothing to do with `cmd.exe`. The process for the console window is `conhost.exe`. – Eryk Sun May 13 '15 at 23:56
1

try this..

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();

Also where is your cmd.exe file? If its not ran out of the same directory that your application is running from? You may need to provide a pathway to point to the cmd.exe file.

example..

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

Hope it helps

Hemi81
  • 578
  • 2
  • 15
  • 34
  • Thanks Marc, this would have worked except it's running as a service and cannot run a command prompt. Per the question above I also tried setting explicit paths. Marc Johnston's solution below was the correct one, though yours would be appropriate for an app that can spawn a command prompt. Thanks! – joelc May 13 '15 at 16:51