שתף באמצעות


Shut down my Computer with a program from Visual Basic

Question

Friday, April 17, 2009 12:06 PM

What is the code to shut down my computer with visual basic?


The End is Near

All replies (9)

Friday, April 17, 2009 12:13 PM ✅Answered | 2 votes

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

Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]


Friday, April 17, 2009 12:14 PM ✅Answered | 1 vote

Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\LocalHost\roo t\cimv2")
ms.Options.EnablePrivileges = True
'ms.Options.Password = ""
'ms.Options.Username = ""
Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim query1 As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher(ms, oq)
Dim queryCollection1 As System.Management.ManagementObjectCollection = query1.Get()
For Each mo As System.Management.ManagementObject In queryCollection1
Dim ss As String() = {"2", "0"} ' Reboot
'Dim ss As String() = {"2", "2"} ' Logoff
'Dim ss As String() = {"0", "0"} ' Shutdown
mo.InvokeMethod("Win32Shutdown", ss)
Next

Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]


Friday, April 17, 2009 12:55 PM ✅Answered | 1 vote

Hi Kaos Warrior,

just searched the forum for "shut down computer" and got the hit http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/993b2d52-41db-41fd-8afb-8bc5dcb46915/

Mark the thread as answered if the answer helps you. This helps others who have the same problem !


Friday, April 17, 2009 1:15 PM ✅Answered | 1 vote

These examples may help, although I haven't tried them on the latest Windows versions.

    http://www.vb-helper.com/howto_shutdown_windows.html
    http://www.vb-helper.com/howto_shutdown_exitwindowsex.html
    http://www.vb-helper.com/howto_shutdown.html

You may have permission problems in Vista so you may have to run with process elevation.

The last one lists some DOS commands that work for Win32 and WinXP. If you find the right commands for your OS, you can probably use ShellExecute to run them.

Rod

Rod Stephens, Visual Basic MVP

Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/


Friday, April 17, 2009 12:34 PM

when i push the button it flashes the command prompt and the other one just errors
The End is Near


Friday, April 17, 2009 1:24 PM

how do i get the code to work for windows xp instead of vista

The End is Near


Friday, April 17, 2009 1:26 PM

without words ...

Dim thePSI as new System.Diagnostics.ProcessStartInfo("shutdown.exe")

thePSI.Arguments = "/m \computerName /s /t 10"

System.Diagnostics.Process.Start(thePSI)

Mark the thread as answered if the answer helps you. This helps others who have the same problem !


Friday, April 17, 2009 1:27 PM

that worked thanks Heslacher for the tip.The End is Near


Friday, April 17, 2009 1:30 PM

that worked thanks Heslacher.The End is Near