6

How to shutdown my computer using C#?

sharptooth
  • 167,383
  • 100
  • 513
  • 979

7 Answers7

10

An easy way: Use Process.Start to run shutdown.exe.

  shutdown /s /t 0

Programmatic way: P/Invoke a call to ExitWindowsEx

This would be the P/Invoke signature:

[DllImport("aygshell.dll", SetLastError="true")]
private static extern bool ExitWindowsEx(uint dwFlags, uint dwReserved);

Under all circumstances, the user running the code will need shutdown system privileges (normally not a problem, but an important point to remember).

driis
  • 161,458
  • 45
  • 265
  • 341
  • Would you say a bit about how a person would choose between the two methods? Or is it just easy vs not so easy? – J M Mar 20 '09 at 09:20
  • I would nromally go for the second option in production software, as it gives you more control (shutdown reasons, etc). It takes a bit longer to implement, since I would need to lookup the correct flag and the signature. – driis Mar 20 '09 at 09:30
5

Different methods:

A. System.Diagnostics.Process.Start("Shutdown", "-s -t 10");

B. Windows Management Instrumentation (WMI)

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=36953

http://www.dreamincode.net/forums/showtopic33948.htm

C. System.Runtime.InteropServices Pinvoke

http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-c

D. System Management

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

After I submit, I have seen so many others also have posted...

lakshmanaraj
  • 4,145
  • 23
  • 12
4

WindowsController is a c# wrapper class around ExitWindowsEx.

Sometimes you need to restart or shutdown the operating system from your applications (for instance, after the installation of a program). The .NET framework offers you an indirect way to restart the computer through the Windows Management Instrumentation (WMI) classes in the System.Management namespace, however, there seem to be some problems in their implementation.

That's why we created the WindowsController class that implements some API functions to restart and shutdown Windows. It supports all the ExitWindowsEx modes and it can also hibernate and suspend the system.

This class is available in C# and VB.NET version. It can be compiled to a .NET module or to a library to be used from other .NET languages. Since it relies on the Windows API, it will not work on Linux or FreeBSD.

(mentalis.org)

Aleris
  • 7,981
  • 3
  • 36
  • 42
1

Use a variation of the "user logoff" code shown here.

That code uses the ExitWindowsEx API call.

Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

You could also use InitiateSystemShutdown

http://www.pinvoke.net/default.aspx/advapi32.initiatesystemshutdown

using System;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    [DllImport( "advapi32.dll" ) ]
    public static extern bool InitiateSystemShutdown( string MachineName , string Message , uint Timeout , bool AppsClosed , bool Restart );

    [DllImport( "kernel32.dll" ) ]
    public static extern uint GetLastError();

    [DllImport( "kernel32.dll" ) ]
    public static extern uint FormatMessage( uint Flags , IntPtr Source , uint MessageID , uint LanguageID , StringBuilder Buffer , uint Size , IntPtr Args );



    public static void Main()
    {
        InitiateSystemShutdown(System.Environment.MachineName, "hello", 0, false, false);
        //InitiateSystemShutdown("localhost", "hello", 0, false, false);

    }
}
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0

At a guess (untested):

Process.Start("shutdown", "-s -t 0");
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

The hard way, works on laptops perfectly, although it takes some time:

Spawn a couple endless loops in more threads than cpu cores.
Wait for overheat which will automatically shutdown a computer.

:)

Marko
  • 30,263
  • 18
  • 74
  • 108