Shutdown, Restart, or Log Off your computer using VB.Net


By Abdul Razzak Fallaha
Printer Friendly Version
  

Here's a quick code sample demonstrating how to use Process.Start in .NET to shutdown, restart, or log off your computer.



At first we have to make a simple form that contains 4 buttons (Text property=Shutdown, Restart, Log Off, and Exit)
simply just like the image below



and I made the name property for these buttons to (name=btnShutdown, btnRestart, btnLogOff, and btnExit) that is to help us using and calling the buttons in the code.

But what about the procedure that will make me do the shutdown process
well we write this in the code for each button:
first we write function that will call the shutdown procedure: System.Diagnostics.Process.Start
Then we call the shutdown procedure: ("shutdown",
after we called it we will have to name the process we want to do:
to make it shutdown: we write after the comma "-s"
and to make it restart we write after the comma "-r"
and to make it logoff we write after the comma "-l"

so the code will be for example:
System.diagnostics.Process.Start("shutdown", "-s")
' Will cause the computer to shutdown


but when you order it to shutdown like this it will give you about 20 sec to shutdown
but there is a way to manage that
System.Diagnostics.Process.Start("shutdown", "-s -t 00")
' The -t will contol the time after -t we write the number of sec (Note: max sec = 99)

Now we make for each one of the buttons its own sub (simply we can do that by double clicking on each button)

The code for the whole program will be as follows


Public Class frmShutdown

    Private Sub btnShutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShutdown.Click
        System.Diagnostics.Process.Start("shutdown", "-s -t 00")
        'This will make the computer Shutdown
    End Sub

    Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
        System.Diagnostics.Process.Start("shutdown", "-r -t 00")
        'This will make the computer Restart
    End Sub

    Private Sub btnLogOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogOff.Click
        System.Diagnostics.Process.Start("shutdown", "-l -t 00")
        'This will make the computer Log Off
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
        'This will make the program to terminate(end the program)
    End Sub

End Class

(I will upload solution file soon so you can download it)
But this function (System.Diagnostics.Process.Start) isn't just for shuting down your computer it call also calls any program or file you want, for example:
System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
' This will call the Internet Explorer

you just have to write the path of the file you want to open between the two quotation marks

Abdul Razzak Fallaha
Your tags are very important to me please write your idias, comments,or tags



button
 
Article Discussion: Shutdown, Restart, or Log Off your computer using VB.Net
  Abdul Razzak Fallaha posted at 02-Dec-07 05:45
Original Article

 
  i think i need shutdown function!!!!!!!
  moheeb patuary replied to Abdul Razzak Fallaha at 23-Dec-07 12:50

The system cannot find the file specified

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified

Source Error:

Line 27:     protected void btnLogOff_Click(object sender, EventArgs e)
Line 28: {
Line 29: Process.Start("shutdown", "-l -t 00");
Line 30: }
Line 31: protected void Exit_Click(object sender, EventArgs e)


 
  I think that you are not using VB.Net
  Abdul Razzak Fallaha replied to moheeb patuary at 25-Dec-07 12:14
The article title says:
Shutdown, Restart,or Log Off your computer using VB.NET(Visual Basic.Net)
You used another programing language.
This article can be applied only on Visual Basic.Net

 
  thanks. is thr any c# code pls.
  moheeb patuary replied to Abdul Razzak Fallaha at 25-Dec-07 11:43
thanks. is thr any c# code pls.

 
  Windows App or Web Page?
  blazingpretzel 1 replied to moheeb patuary at 22-Apr-08 12:21
It appears that your problem is that you are running your code in an ASP.NET web page instead of running it in a Windows application. Procedures such as shutting down a computer or opening programs cannot be executed from there. If you want to create an app, there is probably a C# equivalent to this code.