Solved

Shut Down win Xp vb.net 2008

Posted on 2009-05-07
10
416 Views
Last Modified: 2013-11-26
i need vb.net 2008 code to shut down win xp
0
Comment
Question by:aarontham
10 Comments
 
LVL 5

Expert Comment

by:drakeshe
Comment Utility
Shutdown, Restart, or Log Off your computer using VB.Net
    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

Open in new window

0
 
LVL 7

Expert Comment

by:thiyaguk
Comment Utility
0
 
LVL 5

Expert Comment

by:drakeshe
Comment Utility
oh ya, forgot lol. =/
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
Comment Utility
that was blazingly fast, drakeshe!

Instead of using the shutdown.exe tool (which, iirc, is not available on older windows, but then again, older windows are rare) you can do this through Win32Shutdown method of WMI as follows, which has the benefit of being able to get information when the shutdown does not succeed:

Dim response As Single

Dim objWMIService, objComputer As Object
 

'Now get some privileges '

objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
 

For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")
 

   ' 1 + 4 is a forced shutdown, 1 is a normal shutdown '

   response = objComputer.Win32Shutdown(1 + 4, 0)
 

   If response <> 0 Then

      MsgBox("Something wrong")

   Else
 

   End If

Next

Open in new window

0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Tx for the grade & pts, but...

if you don't comment on why you accept one solution over another, while another is still a
viable alternative, it is a common use at EE to split the points. See the closing q. guidelines.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:aarontham
Comment Utility
drakeshe solution don't work on win XP
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
ah, ok. Didn't know that, tx for letting us in it ;)
0
 
LVL 5

Expert Comment

by:drakeshe
Comment Utility
It does work on Windows XP, but perhaps you do not have proper permissions. If the methods are controlled by permissions then it may not be a good solution
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
interesting point, but the permissions are just as needed when you use the WMI method. The only difference may be that you can interrogate the return value to find out why restarting / shutting down didn't work.

But there's something else with shutdown.exe, or with any other Process.Start. More and more restrictions are applied on starting other processes. More firewalls bug users with messages "do you allow this process X to start this other process Y". It is for the good, but sometimes it is working quite too much against us.
0
 
LVL 5

Expert Comment

by:drakeshe
Comment Utility
I've heard of anti-viruses that disabled .exe files that aren't installed into c:\program files\*  XD
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now