sari k
sari k

Reputation: 2111

How can run functions of powercfg by C# code?

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 

Upvotes: 2

Views: 3351

Answers (3)

SLaks
SLaks

Reputation: 887469

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

Upvotes: 5

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

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

Upvotes: 1

Oded
Oded

Reputation: 499062

Call it with Process.Start:

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

Upvotes: 6

Related Questions