5
Reply

Code to hibernate PC

Peter Malone

Peter Malone

Jan 18 2011 4:18 PM
13.2k
I have the following code under a logoff event in a Windows app I have written:
  if (e.Reason == SessionEndReasons.Logoff)

{

string command = string.Format(@"/c POWERCFG /CHANGE /hibernate-timeout-dc 1");

ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");

cmdsi.WindowStyle = ProcessWindowStyle.Hidden;



cmdsi.Arguments = command;

Process cmd = Process.Start(cmdsi);





It changes the power options to make the PC go to hibernate one minute after the user logs off. 

If you copy and paste this part of the code into a dos window you will see the change in Windows power options

 POWERCFG /CHANGE /hibernate-timeout-dc 1

It works OK when I install the app and then log off. But if I log back on and log off after that it gives me "the application was unable to start" error and an error code.
I can only log off once for it to work. Restarting doesn't fix it.
Would appreciate any help, thanks.


Answers (5)