How to shutdown my computer using C#?
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).
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.
:)
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...
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)