joelc
joelc

Reputation: 2761

Reboot Windows from within a service

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.

Upvotes: 0

Views: 957

Answers (2)

Hemi81
Hemi81

Reputation: 588

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

Upvotes: 1

Marc Johnston
Marc Johnston

Reputation: 1286

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

Upvotes: 1

Related Questions