精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
// 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);
http://www.geekpedia.com/tutorial177_Lock-Stand-By-Hibernate-and-Log-Off.html
One good article: http://www.geekpedia.com/tutorial177_Lock-Stand-By-Hibernate-and-Log-Off.html推荐文章。
I just got the article and updated the answer. You beat me to it. 我只是写了篇文章,更新答案。被你打败。
I suggested a ***very*** alternative method, please see. 我建议 * * * 很 * * * 替代方法,请参阅。
This is a cheat sheet of commands for shutdown (hibernate, lock, stand by…), reboot and logoff:
这是一张备忘单的关机命令 (有休眠、 锁定、 准备...),重启和注销:
http://www.robvanderwoude.com/shutdown.php
You can simply use any of this command on System.Diagnostics.Process.Start. Maybe a bit dirty but fastest to implement; also can be used in batch files, not just in your application; a batch file can be used as a very quick test of the command line.
你能简单地在 System.Diagnostics.Process.Start 上使用任何此命令。也许有点脏,但这是最快的实现;也可以用在批处理文件中,不只是在您的应用程序;批处理文件可以用作快速测试命令行。
Very good alternative, +5 In my opinion it's not even dirty since nevertheless you have to use user32.dll from managed code.
很好的选择,我认为加5分,它甚至不是肮脏的因为必须从托管代码使用 user32.dll
Thank you Mika. It is dirty in the following sense: if you mistype some part of command line, the code will not work without any explanation; also, it will work in a separate process; from the other hand, it's easy to debug -- via a batch file. By the way, it's not really using user32.dll, it's using shell; all other executables go in different process.
谢谢你米卡。以下面的描述想,它是脏的: 如果您键入了错误的命令行部分,代码将不工作,没有任何解释;此外,它将工作在一个单独的进程;从另一个角度看,很容易调试 — — 通过一个批处理文件。顺便说一句,它不是真的使用 user32.dll,它利用壳;所有其他可执行文件安排在不同的进程中。
However, wait a minute, P/Invoke also use constant string; you also can mistype it. So, yes, the solution makes sense.
然而,等待一分钟,P/Invoke 还使用常量的字符串;也可以键入它。所以,解决办法是有道理的。
That's true. Although you can always redirect stderr and stdout, nothing beats a good command line error message.
这是事实。虽然你总是可以重定向标准输出和标准错误输出,没有什么比命令行错误消息好。