This will explain how to Shutdown computer using VB.Net. Using VB.NET source code we can shutdown, restart, or log off a computer with few lines of code.
Shut down the PC:
In VB.NET, you can use the System.Diagnostics.Process class to execute the “shutdown” command in the command prompt, which will shut down the computer. Here’s an example of how to do this:
Imports System.Diagnostics
Module Module1
Sub Main()
' Use the Process class to execute the "shutdown" command
Dim proc As New Process()
proc.StartInfo.FileName = "shutdown"
proc.StartInfo.Arguments = "-s -t 0" ' -s for shut down, -t for time in seconds
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
End Sub
End Module
The above code will execute the “shutdown -s -t 0” command in the command prompt, which will initiate a shutdown of the computer with a time of 0 seconds. If you want to have a delay before the shutdown you can replace 0 with the desired time in seconds.
You can also use the “shutdown.exe” command instead of “shutdown”, the result will be the same.
It’s important to note that this code will shut down the computer immediately, without giving the user a chance to save their work or cancel the shutdown. Be careful when using this code, and make sure to give the user adequate warning before shutting down their computer.
Restart the PC:
In VB.NET, you can use the System.Diagnostics.Process class to execute the “shutdown” command in the command prompt, which will restart the computer. Here’s an example of how to do this:
Imports System.Diagnostics
Module Module1
Sub Main()
' Use the Process class to execute the "shutdown" command
Dim proc As New Process()
proc.StartInfo.FileName = "shutdown"
proc.StartInfo.Arguments = "-r -t 0" ' -r for restart, -t for time in seconds
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
End Sub
End Module
The above code will execute the “shutdown -r -t 0” command in the command prompt, which will initiate a restart of the computer with a time of 0 seconds. If you want to have a delay before the restart you can replace 0 with the desired time in seconds.
You can also use the “shutdown.exe” command instead of “shutdown”, the result will be the same.
It’s important to note that this code will restart the computer immediately, without giving the user a chance to save their work or cancel the restart. Be careful when using this code, and make sure to give the user adequate warning before restarting their computer.
Log off the PC:
In VB.NET, you can use the System.Diagnostics.Process class to execute the “shutdown” command in the command prompt, which will log off the computer. Here’s an example of how to do this:
Imports System.Diagnostics Module Module1 Sub Main() ' Use the Process class to execute the "shutdown" command Dim proc As New Process() proc.StartInfo.FileName = "shutdown" proc.StartInfo.Arguments = "-l" ' -l for log off proc.StartInfo.UseShellExecute = False proc.StartInfo.RedirectStandardOutput = True proc.Start() End Sub End Module
The above code will execute the “shutdown -l” command in the command prompt, which will initiate a log off of the current user.
It’s important to note that this code will log off the computer immediately, without giving the user a chance to save their work or cancel the log off. Be careful when using this code, and make sure to give the user adequate warning before logging off their computer.
You can also use “rundll32.exe user32.dll,LockWorkStation” instead of “shutdown -l”, the result will be the same.