C# put pc to sleep or hibernate

Sandeep Bansal picture Sandeep Bansal · Jan 17, 2010 · Viewed 25.1k times · Source

I want to put my system to either sleep or hibernate, two different options.

How would I do this with API's, I don't really want to use Process, and that doesn't allow me to choose what method I want for this action.

Answer

fre0n picture fre0n · Jan 17, 2010
// Hibernate
Application.SetSuspendState(PowerState.Hibernate, true, true);
// Standby
Application.SetSuspendState(PowerState.Suspend, true, true);

Or, if you like system calls:

[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);