Reboot machine from a C#/WPF app

Josh Santangelo picture Josh Santangelo · Aug 1, 2009 · Viewed 41.5k times · Source

I want to have a button in my WPF app that restarts the machine. This app is always running on Vista.

The fact that a quick search hasn't turned anything up makes me think this might be harder than I wish it was... any ideas? Thanks!

Answer

Lucas Jones picture Lucas Jones · Aug 1, 2009

Try this:

System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0");

This starts Windows' inbuilt shutdown tool, which can also shut down or logoff a remote or the local machine.

Here is the list of full options from ss64.com:

Syntax

      SHUTDOWN [logoff_option]  [/m \\Computer] [options]

logoff_option:
    /i         Display the GUI (must be the first option)
    /l         Log off. This cannot be used with /m or /d option
    /s         Shutdown
    /r         Shutdown and Restart
    /a         Abort a system shutdown.
               (only during the time-out period)
    /p         Turn off the local computer with no time-out or warning
               (only with /d)
    /h         Hibernate the local computer (only with /f )
    /e         Document the reason for an unexpected shutdown of a computer

Options:

   /m \\Computer  : A remote computer to shutdown.

   /t:xxx         : Time until system shutdown in seconds. 
                    The valid range is xxx=0-600 seconds. [default=30]
   /c "Msg"       : An optional shutdown message [Max 127 chars]

   /f             : Force running applications to close.
                    This will not prompt for File-Save in any open applications.
                    so will result in a loss of all unsaved data!!!

   /d u:xx:yy     : List a USER reason code for the shutdown. 
   /d P:xx:yy     : List a PLANNED reason code for the shutdown.
                     xx Specifies the major reason code (0-255)
                     yy Specifies the minor reason code (0-65536)

You'll probably notice that I have used the Linux/UNIX style of passing command-line arguments (using the '-') sign. On Windows, the convention is using '/'. This doesn't matter - the program doesn't care.