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

.NET code sample for shutdown, restart, or log off a computer using Process.Start in VB.NET or C#.

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

By Abdul Razzak Fallaha   Popularity  (42285 Views)