Thursday, May 27, 2010

Shutdown the Computer

To shut the computer down by a C# - program, we just call the native to Windows program "shutdown".
When entering this command in the command prompt (cmd.exe), e.g. with the parameters shutdown -s -t 60, the computer will shut down after the elapse of 60 seconds.
Entering this command actually calls the program "shutdown.exe", which is located in the system32 folder.
. To start an external programm we use the function System.Diagnostics.Process.Start(), like described in a previous post.
In the program we now can for example enter
System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-s -t 60");

When executing this code, a message will pop up saying the computer is shutting down in 60 seconds.
Finally, some further possible parameters for "shutdown.exe":
  • -l: Log out
  • -s: Shutdown
  • -r: Restart
  • -a: Abort
  • -t XX: Time
  • -c: Prints a comment von shutting down
  • -f: Forces the shutdown (closes all open programs)

No comments:

Post a Comment