Home > Backend Development > C++ > How Can I Shut Down a Windows Computer Using C#?

How Can I Shut Down a Windows Computer Using C#?

Barbara Streisand
Release: 2025-01-18 14:38:09
Original
350 people have browsed it

How Can I Shut Down a Windows Computer Using C#?

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template