Categories

How to Shutdown, Restart, Log off your computer with VB.NET or C#

This is extremely easy using VB.NET or C# (pronounced C sharp) and the built in windows EXE file shutdown.exe. To shutdown, restart, or log off your computer is as simple as entering 1 line into your program, it just so happens that the 1 line is identical whether you’re using VB.NET or C#.

To shutdown/turn off your computer, just execute the following line of code:

1
System.Diagnostics.Process.Start("shutdown.exe", "-s -t 0")

To restart/reboot your computer, just execute the following line of code:

1
System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0")

To log off your computer, just execute the following line of code (and yes, that’s a lowercase L, not the number 1):

1
System.Diagnostics.Process.Start("shutdown.exe", "-l -t 0")

The number zero at the end of each of those examples is how long the computer should wait in seconds before shutting down/restarting/etc… In this example, I’m going to shutdown the computer in 5 minutes (5 minutes * 60 seconds = 300 seconds)

1
System.Diagnostics.Process.Start("shutdown.exe", "-s -t 300")

2 comments to How to Shutdown, Restart, Log off your computer with VB.NET or C#

  • by using above code my system not getting shutdown or restart instead its creating new child form

  • Sam

    I tried this line of code in my C# compiler, and when used both in debug and the exe, nothing happened other than CMD flashing up for a split second. No shutdown occurred.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

eXTReMe Tracker