如何通过c#代码运行powercfg的功能?
例如,我想运行它,为Set关闭显示:never
powercfg -CHANGE -monitor -timeout -ac 0
Run Code Online (Sandbox Code Playgroud)
称之为Process.Start:
Process.Start("powercfg", "-CHANGE -monitor -timeout -ac 0");
Run Code Online (Sandbox Code Playgroud)
您可以调用Process.Start以运行可执行文件.
例如:
Process.Start(fileName: "powercfg", arguments: "-CHANGE -monitor -timeout -ac 0");
Run Code Online (Sandbox Code Playgroud)
但是,如果您只是在程序运行时尝试禁用自动关闭,则应该处理该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);
}
Run Code Online (Sandbox Code Playgroud)