How to Shut Down the Computer from C#

How to shut down the computer from C#

Taken from: a Geekpedia post

This method uses WMI to shutdown windows.

You'll need to add a reference to System.Management to your project to use this.

using System.Management;

void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();

// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");

// Flag 1 means we want to shut down the system. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}

Be sure C# code executes before shutdown

You can attach a handler to the SystemEvents.SessionEnded event and run your code there. You can even cancel the shutdown / logoff event using the Cancel property.
The same code could be run on the FormClosing event but with CloseReason == CloseReason.UserClosing || CloseReason == CloseReason.ApplicationExitCall || CloseReason == CloseReason.TaskManagerClosing to cover all scenarios.

How to shut down the computer from C#

Taken from: a Geekpedia post

This method uses WMI to shutdown windows.

You'll need to add a reference to System.Management to your project to use this.

using System.Management;

void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();

// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");

// Flag 1 means we want to shut down the system. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}

Exe program that shutdown Windows?

"I want to make an exe app that shutdown Windows."  

These code examples (Both Linux and Windows.) are bare-boned examples from the link below. You will have to consider that the programs need to be executed with sufficient privileges, along with several other things for them to work as you would like...

In Windows:

EDIT (after tag change from C to C#

C# -
you can use this with any Windows utility exe:

Process.Start("application.exe", "param1 param2");

eg.

Process.Start("c:\\windows\\system32\\shutdown.exe", "/s");

For ANSI C -

#include <stdio.h> 
#include <stdlib.h>

int main(void)
{
system("c:\\windows\\system32\\shutdown /s"); //simple shutdown
return 0;
}

Other commands:

system("c:\\windows\\system32\\shutdown /i"); //shutdown with GUI
system("c:\\windows\\system32\\shutdown /r"); //Restart
system("c:\\windows\\system32\\shutdown /l"); //Logoff
Note: These Windows shutdown commands may bring up a dialog
box requiring user to enter PC name. If you want to automate
populating the dialog box, command switches will help:
  Open a cmd prompt in Windows and type "help shutdown" to get this information

More information here



Related Topics



Leave a reply



Submit