views:

511

answers:

6

How to shutdown my computer using C#?

+10  A: 

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
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
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
A: 

At a guess (untested):

Process.Start("shutdown", "-s -t 0");
Marc Gravell
+1  A: 

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

That code uses the ExitWindowsEx API call.

Alnitak
A: 

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.

:)

Dev er dev
So many downvotes! Was it just me who found the answer amusing?
Marcel Tjandraatmadja
amusing, so +1. Remember:All work and no play makes Jack a dull boy..
Peter Perháč
+5  A: 

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
+1 for options and links
J M
Although saying that I am still thinking - when would a person not just usethe first option?
J M
I am not sure whether some one can disable rights for execution of shutdown.exe, in that case, one might need code to execute...
lakshmanaraj
aren't B and D the same method? (WMI)
Lucas
@Lucas, Eventhough both are WMI, invoking method names differ from each other.
lakshmanaraj
+2  A: 

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