C# Simple method to shut down the computer gracefully
Want to easily shut down your computer in a C# program? Although there are many methods, not many can combine simplicity and native .NET functionality.
For Windows XP and above, there is a simple solution:
<code class="language-csharp">Process.Start("shutdown","/s /t 0");</code>
This command will shut down the computer immediately without any delay.
If you need compatibility with Windows 2000 or earlier, you need to use P/Invoke or WMI. However, if you want a more flexible approach, consider the following code:
<code class="language-csharp">var psi = new ProcessStartInfo("shutdown","/s /t 0"); psi.CreateNoWindow = true; psi.UseShellExecute = false; Process.Start(psi);</code>
This code avoids ugly windows during shutdown and ensures a smooth experience.
The above is the detailed content of How Can I Shut Down a Windows Computer Using C#?. For more information, please follow other related articles on the PHP Chinese website!