Shutting down Windows with C#

Shutting down Windows with C#

This article describes how to shut down your machine using C# code. There are a few approaches to do that in C# but I will describe the easiest one, which works on both Win XP and Vista. All you have to do is just to make sure that you don’t have any additonal threads running, otherwise your machine may refuse to shut down (spotted this problem on XP).
Here you have a short snipped which will do the work for you:

                System.Diagnostics.Process process = new Process();
                process.StartInfo.FileName = "SHUTDOWN";
                process.StartInfo.Arguments = "-s -t 05"; // -r is for restart, -s is for shutdown
                process.Start();

After this call if you have a windows forms application you can call Dispose() on the main form to exit nicely.

One response on “Shutting down Windows with C#

Leave a Reply