Shutdown or Restart the Computer Using C#

| January 8, 2009 | 6 Comments

The easiest way to shutdown, restart, or hibernate a computer programmatically using C# is to use the shutdown command-line tool that comes with Windows. This tool has a lot of options, for example /l logs of the user, /s shuts down the computer, /r restarts the computer, and /h hibernates the computer. To get the full list of available options, type help shutdown in the Command Prompt.
To execute shutdown from C# use the follow statement:

//the first parameter is the command, and the second is its arguments
System.Diagnostics.Process.Start("shutdown", "/s");

 

Download Source Code

 


Related Posts:

Filed Under: WinForms

Follow us on Twitter

Comments

  1. deepak baldia says:

    Code to Shutdown or Restart the Computer Using C#

    ProcessStartInfo startinfo = new ProcessStartInfo(“shutdown.exe”, “-s”);
    Process.Start(startinfo);

  2. K TEJ KUMAR says:

    I hope it works and thanks for the contribution

  3. Fazil says:

    could you please tell me the proper syntax for shutdown….whenever i try to do this, it shows this error ” The type or namespace name ‘ProcessStartInfo’ could not be found (are you missing a using directive or an assembly reference?) “

  4. pankaj says:

    it’s working …

    thanks

  5. Ahmad Moosa says:

    “are you missing a using directive or an assembly reference”
    this is your problem exactly as you have to reference System.Diagnostics in order to use ProcessStartInfo class :)

  6. sukshith says:

    use the assembly reference as shown below:It works

    System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo(“shutdown.exe”, “-s”);
    System.Diagnostics.Process.Start(startinfo);

Leave a Reply