C# Program to Shutdown and Restart a Computer

In some circumstances, you may need to allow your program to restart or shut down a computer that uses a Windows Operating System. This article will teach you how to shut down and restart a computer using C#.

Restart and shut down a computer using C#

To begin with, you will need to add the namespace below:

using System.Diagnostics;

The function below is used to restart or shut down a windows operating system. It calls the shutdown windows command by using the Process.Start function from the System.Diagnostics namespace.

void ShutdownRestart(bool restart = false) {
  if (restart) {
    Process.Start("ShutDown", "/r"); // To restart a windows OS
  } else {
    Process.Start("ShutDown", "/s"); // To shutdown a windows OS


Note that the shutdown.exe file is located in the location below:

Now you can call the function above by passing the restart argument to get the desired action:

public static void Main(String[] args) 

Timed shutdown using C#

Timed shutdown allows you to run the shutdown command after a specific time. You can use timed shutdown by adding the time when calling the command:

Process.Start("shutdown","/s /t 60");

The Windows Operating System will be shut down after 1 minute in the code above.

Bonus – Logoff user using C#

The shutdown command can be used to logoff a logged-in user by simply passing the argument as shown below:


Happy coding!


Leave a Reply

Your email address will not be published. Required fields are marked *