Using C# to Power Down Your Windows Computer
Several methods exist for shutting down a Windows computer from a C# application. The most efficient and integrated .NET solution involves using Process.Start
with the shutdown
command.
For Windows XP and later, a simple command suffices:
<code class="language-csharp">Process.Start("shutdown", "/s /t 0");</code>
This immediately initiates a system shutdown (0 seconds timeout).
For controlled shutdowns, adjust the /t
parameter to specify the desired delay in seconds:
<code class="language-csharp">Process.Start("shutdown", "/s /t 120"); // Shuts down after 120 seconds</code>
To avoid displaying a separate shutdown window, utilize this refined code:
<code class="language-csharp">var psi = new ProcessStartInfo("shutdown", "/s /t 0"); psi.CreateNoWindow = true; psi.UseShellExecute = false; Process.Start(psi);</code>
This ensures a clean, windowless shutdown.
The above is the detailed content of How to Shut Down a Windows Computer Using C#?. For more information, please follow other related articles on the PHP Chinese website!