Shutting down Windows with C#
Posted by admin | Posted in Code Snippets | Posted on 23-04-2009
1
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();
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.





[...] using the registry settings. This article is somehow opposite to the one posted earlier about automatic shutdown. To set the registry for your application to start up automatically you need to find the key [...]