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

How to Shut Down a Windows Computer Using C#?

Susan Sarandon
Release: 2025-01-18 14:26:10
Original
900 people have browsed it

How to Shut Down a Windows Computer Using C#?

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

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

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

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!

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