5

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

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • 2
    To what end? If this is "whilst my application is doing X, the system should stay available", be aware that changing the machine settings is the wrong way to do this. – Damien_The_Unbeliever Aug 11 '20 at 07:30
  • @Damien_The_Unbeliever The machine will be used as a kiosk. Therefore, the installer needs to set this value. – AngryHacker Aug 11 '20 at 15:34

1 Answers1

7

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: