AngryHacker
AngryHacker

Reputation: 61596

How to set Power and Sleep settings?

I'd like to change Screen & Sleep time to Never from code. I'm seeing code that prevents the Screen Saver from executing, but don't see anything for Screen & Sleep timers.

Is this possible?

enter image description here

Upvotes: 6

Views: 2231

Answers (1)

41686d6564
41686d6564

Reputation: 19641

The easiest way would be to use the Powercfg utility.

Here are some useful options (xx = minutes):

  • Timeout to turn off the display (plugged in): monitor-timeout-ac xx.
  • Timeout to turn off the display (battery): monitor-timeout-dc xx.
  • Timeout to go to sleep (plugged in): standby-timeout-ac xx.
  • Timeout to go to sleep (battery): standby-timeout-dc xx.
  • Timeout to go into hibernate (plugged in): hibernate-timeout-ac xx.
  • Timeout to go into hibernate (battery): hibernate-timeout-dc xx.

Full example:

// Set the monitor timout to "never".
var info = new ProcessStartInfo("cmd", "/C powercfg /change -monitor-timeout-ac 0");
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);

Alternatively, you may use WMI to achieve the same goal. Here are some related posts:

Upvotes: 8

Related Questions