如何通过 c# 代码运行 powercfg 的功能?
例如我想运行这个,设置关闭显示:从不
powercfg -CHANGE -monitor -timeout -ac 0
调用它Process.Start
:
Process.Start("powercfg", "-CHANGE -monitor -timeout -ac 0");
您可以调用Process.Start
以运行可执行文件。
例如:
Process.Start(fileName: "powercfg", arguments: "-CHANGE -monitor -timeout -ac 0");
但是,如果您只是在程序运行时尝试禁用自动关闭,则应WM_SYSCOMMAND
改为处理该消息。
例如:
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);
}
您可以使用Process
该类从 C# 运行 powercfg。