Sleep/Hibernate the system using C#

I was in the process of building an application that requires putting the system to sleep or hibernate depending on the option that the user selected. I found a very simple way to add this functionality to my existing application.

Let's get straight in to it, simply using the following code will allow you to Sleep or Hibernate your system.

// Hibernate
Application.SetSuspendState(PowerState.Hibernate, true, true);

// Standby
Application.SetSuspendState(PowerState.Suspend, true, true);

If you would like to use Interop Services then the following implementation will be helpful

[DllImport("Powrprof.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

// Hibernate
SetSuspendState(true, true, true);
// Standby
SetSuspendState(false, true, true);

Sandeep Bansal is a Software Engineer focusing on Microsoft and Web technologies. He looks to develop both well designed and functional desktop/phone applications.

Similar Posts you might like