Make Windows shutdown, reboot, log off, lock, hibernate, and sleep in C#

[shutdown]

This example shows how a C# program can make Windows shutdown, reboot, lock, log off, or hibernate. Some of those operations require you to use methods defined in external libraries, so the code uses DllImport attributes to define them. DllImport is defined in the InteropServices namespace, so the code includes this using statement:

using System.Runtime.InteropServices

The program also uses processes, so it includes the following using statement:

using System.Diagnostics

The following DllImport statements go inside the class to define the external ExitWindowsEx and LockWorkStation methods.

[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

[DllImport("user32")]
public static extern void LockWorkStation();

With all of that set up, the code is reasonably simple. The following code shows how the program Windows shutdown, reboot, log off, lock, hibernate, and sleep.

// Shutdown.
private void btnShutdown_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo("shutdown", "/s /t 0");
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    Process.Start(psi);
}

// Reboot.
private void btnReboot_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo("shutdown", "/r /t 0");
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    Process.Start(psi);
}

// Log off.
private void btnLogOff_Click(object sender, EventArgs e)
{
    ExitWindowsEx(0, 0);
}

// Lock.
private void btnLock_Click(object sender, EventArgs e)
{
    LockWorkStation();
}

// Hibernate.
private void btnHibernate_Click(object sender, EventArgs e)
{
    Application.SetSuspendState(PowerState.Hibernate, true, true);
}

// Sleep.
private void btnSleep_Click(object sender, EventArgs e)
{
    Application.SetSuspendState(PowerState.Suspend, true, true);
}

This all works well, except if you’re trying to shutdown or reboot. In those cases, if programs are running, the system displays a message asking the user whether it should continue or cancel so the user can close those programs. That’s reasonable, but it would be nice to have the option of disabling that message. (If you figure out how to do that, please let me know.)

Note also that most programs really shouldn’t be performing these operations because Windows has tools that handle them just fine.

It’s also interesting that Application class has methods to make Windows hibernate or sleep, but not to perform any of the other operations. Perhaps Microsoft will add those capabilities, but I doubt it. The other operations seem more drastic and Microsoft has been notably inconsistent in moving API features into the .NET Framework.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in miscellany, system and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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