[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

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

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 the 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.