C# 使电脑进入睡眠或休眠状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2079813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:20:01  来源:igfitidea点击:

C# put pc to sleep or hibernate

c#hibernatesleep

提问by Sandeep Bansal

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.

我将如何使用 API 做到这一点,我真的不想使用 Process,这不允许我选择我想要的这个操作的方法。

采纳答案by fre0n

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