C# put pc to sleep or hibernate

2019-01-22 06:58发布

问题:

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.

回答1:

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