Shutdown, Restart, or Log Off your computer using VB.Net
By Abdul Razzak Fallaha
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 shutdownbut 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
Popularity (32161 Views)
Article Discussion: Shutdown, Restart, or Log Off your computer using VB.Net
i think i need shutdown function!!!!!!!
moheeb patuary replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
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
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 Wednesday, December 05, 2007 1:56 PM
thanks. is thr any c# code pls.
Windows App or Web Page?
blazingpretzel 1 replied
to moheeb patuary at Wednesday, December 05, 2007 1:56 PM
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.
command is right but i dont want the command prompt.....
shri b replied
to blazingpretzel 1 at Wednesday, December 05, 2007 1:56 PM
i need to execute that command hiding the command prompt,command is right but i dont want the command prompt.....
Access process on LAN connected machines
sourav kumar replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
Hi,
Can you give me an idea or code in c# or vb to access the process of another machines that are connected in LAN or in same work group.
shut down Code
santosh behera replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
hi
i am trying with this shut down,restart,logoff but it is not working.
it's throwing error like(The system cannot find the file specified).
please send me solution.
with regards
santosh
Finding the file
The problem is that it is not finding the file this way so instead of typing the command like this:
System.Diagnostics.Process.Start("shutdown", "-s -t 00")
you have to type it this way:
System.Diagnostics.Process.Start("C:\WINDOWS\system32\shutdown.exe", "-s -t 00")
That's where the file is.
As i said in the end of my article you can type the whole address.
vb.net
Shayan Abbas replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
thanks for such a great tutorial
I want to know How to do following thing through vb.net
1) Create file
2) Delete File
3) Hide & unhide file
4) lock the file ( I learned to lock folder via dos in which we rename the folder via dos by character
we press ALT + 1+5+3+2+4+6 & it rename the folder to some character which I cant be able to write here
& when we open it from window It gives error that it cant be open & to unlock it we have to again rename it from DOS
please give any way to Lock & unlock the folder & also explain it How it works
5) Rename any file
6) Shutdown any PC in my Lan network
7) Shutdown all PC in my Lan network
& please also explain How it works
thankyou
Bookmarked!
Apology to Shayan Abbas
Thank you for your reply, but I am not actually good with these commands.
But the MSDN (the help) that comes with the programing language is very rich.
Try to search for these commands in the MSDN and I assure you will find what you want.
Thank you again for your reply, and I really apologize for not being able to help.
Best Regards Abdul Razzak Fallaha.
john replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
what are the codes if you make a system log out and log in?
You only have to replace the -s (shutdown command) to -l (shutdown command)
So it will look like this:
System.diagnostics.Process.Start("shutdown", "-l")
GameOver replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
Assalam-o-Alaikom,
how to hibernate client system....
Nick replied
to GameOver at Wednesday, December 05, 2007 1:56 PM
To make a computer go into hibernate in VB, type the following code
System.Diagnostics.
Process.Start("shutdown", "-h")
Alfred replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
I WOULD LIKE YOU TO HELP ME TO START AND SHUTDOWN A COMPUTER, AND THE ERRORS THAT MAY OCCUR WHEN STARTING A COMPUTER. IAM WRITTING ON WEDNEYSDAY PLEASE HELP.
Alfred replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
I WOULD LIKE YOU TO HELP ME TO START AND SHUTDOWN A COMPUTER, AND THE ERRORS THAT MAY OCCUR WHEN STARTING A COMPUTER. IAM WRITTING ON WEDNEYSDAY PLEASE HELP.
pavan replied
to santosh behera at Wednesday, December 05, 2007 1:56 PM
I am trying your code written for shutdown, restart, logoff... but its not working, when i execute your code it was opening another form... can u please tell me the reason
Markdean replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
Thumbs up, Sir! Thanks for that very helpful article. I am actually creating a system using vb 2008. I am hoping you could help me more because I will be asking some questions regarding my systems development from time to time. Keep up!
thams replied
to Nick at Wednesday, December 05, 2007 1:56 PM
Does it really work? I mean shutdown -h
I don't think so. B'can even in windows command prompt there is no option to hibernate the system.
You have to install "PS tools" and then you can use that commands here to hibernate.
Eddy Wissels replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
I use the code
process = System.Diagnostics.Process.Start("Shutdown", "-l -t 00") in a service.
The class has FullTrust.
No exception is raised. Still, the function is not executed.
The service assembly is build with Local system.
Deep in the process, I see the process is exited. "Access denied"
Where can I dig, to find an solution to this problem.
Thanks,
Eddy Wissels
wissels@telenet.be
To stop a running program
dinesh replied
to Eddy Wissels at Wednesday, December 05, 2007 1:56 PM
hi frnds.. am a student.. can u help me to stop the running program using vb.net
Yassar replied
to dinesh at Wednesday, December 05, 2007 1:56 PM
dim pProcess() as Process = System.Diagnostics.Process.GetProcessByName("Your Program Name without extension")
For Each abc as Process in pProcess
abc.Kill()
Next
Saad replied
to pavan at Wednesday, December 05, 2007 1:56 PM
To get source code in c#.net for shutdown,logoff and restart command, visit this cool link:
http://bitsbyta.blogspot.com/2011/02/shutdownlogoff-and-restart-commandscnet.html
You suck!
mmmmmm replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
Abdul, you suck!
What a crap piece of code you newbie present.
First, you should know that shutdown.exe does not work in many circunstances, such as all normal computers that does not run poorly configured as admin accounts.
Second, your code dont even check for permissions, so it will have absolutly no feedback about if it worked or not.
What a waste of time, reposting this shit you found somewhere else.
A LESSON
Now i don't know who you are, or were you came from, But i know that you need to learn how to talk!
its ok to criticise people but in a more polite way.
i know that my code is not complete, but i just made it like this to make it simple for beginners.
and it works perfectly.
Please learn how to talk, and then come to negotiate!
Thank you mmmmmm for your comment, and have a nice day!
just replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
your face is EVERYWHERE!!!!!!!!!!!!!!!!
Sachin replied
to Abdul Razzak Fallaha at Wednesday, December 05, 2007 1:56 PM
U R code is very short and use fulllllllllll
just i want say thanksssssss