How can run functions of powercfg by C# code?

强颜欢笑 提交于 2019-12-24 03:25:21

问题


how can run functions of powercfg by c# code?
for example I want to run this, for Set turn off the display: never

powercfg -CHANGE -monitor -timeout -ac 0 

回答1:


Call it with Process.Start:

Process.Start("powercfg", "-CHANGE -monitor -timeout -ac 0");



回答2:


You can call Process.Start to run an executable.

For example:

Process.Start(fileName: "powercfg", arguments: "-CHANGE -monitor -timeout -ac 0");

However, if you're only trying to disable auto-off while your program is running, you should handle the WM_SYSCOMMAND message instead.

For example:

protected override void WndProc(ref Message m) {
    const int SC_SCREENSAVE = 0xF140, SC_MONITORPOWER = 0xF170;
    const int WM_SYSCOMMAND = 0x0112;

    if (m.Msg == WM_SYSCOMMAND) {
        if ((m.WParam.ToInt64() & 0xFFF0) == SC_SCREENSAVE || (m.WParam.ToInt64() & 0xFFF0) == SC_MONITORPOWER) {
            m.Result = 0;
            return;
        }
    }
    base.WndProc(ref m);
}



回答3:


You can use the Process class to run powercfg from C#.



来源:https://stackoverflow.com/questions/4268522/how-can-run-functions-of-powercfg-by-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!