Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-01-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Shutdown, Restart, or Log Off your computer using VB.Net
  

Here's a quick code sample demonstrating how to use Process.Start in .NET to shutdown, restart, or log off your computer.

Simply design the form as image given below and use the given code.



The code for the whole program will be as follows


Code:
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

Reply With Quote
  #2  
Old 15-01-2009
Member
 
Join Date: Jan 2009
Posts: 9
Re: Shutdown, Restart, or Log Off your computer using VB.Net

Thanks,

I was searching for this type of code from long time.
and this really works fine.

Thanks again...........
Reply With Quote
  #3  
Old 15-01-2014
Member
 
Join Date: May 2009
Posts: 620
Re: Shutdown, Restart, or Log Off your computer using VB.Net

There are few set of codes to execute to switch of your system through vb.net. Some of this parameters are quiet similar to what you use in cmd. So for turning off your computer through vb.net you just have to execute "System.Diagnostics.Process.Start("shutdown.exe", "-s -t 0")". Here in place of 0 you can add the seconds. This will delay the shutdown. In the same way if you want to reboot then you can to execute "System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0")". Again here you can see the parameters are quiet similar. You can also delay the reboot through adding a few seconds.
Reply With Quote
  #4  
Old 16-01-2014
Member
 
Join Date: May 2009
Posts: 525
Re: Shutdown, Restart, or Log Off your computer using VB.Net

I am using "System.Diagnostics.Process.Start("shutdown.exe", "-l -t 0")" to logoff. This is quiet simple. Here the parameters are quiet straight. For turning off you require to place Process.Start("shutdown","-s") in the code. For restarting the system you have to add Process.Start("shutdown","-r") and for log off you can use Process.Start("shutdown","-l"). So this makes your job a bit simple. I am not able to find anything about hibernation. I am still looking for code samples that can help me to do more with vb.net.
Reply With Quote
  #5  
Old 20-01-2014
Member
 
Join Date: Dec 2013
Posts: 24
Re: Shutdown, Restart, or Log Off your computer using VB.Net

This is divine! I am trying to build a stand alone program that can be used for reserving tickets and booking seats, building snyposis etc.. I originally did it using VB but have to convert it to VB .NET

A lot of work!
Reply With Quote
  #6  
Old 11-02-2014
Member
 
Join Date: Jan 2014
Posts: 6
Re: Shutdown, Restart, or Log Off your computer using VB.Net

For Hibernating your computer use the following method:

VB.net code:

1) Check for Hibernate Support.

a) Declare the Function
Code:
Public Declare Function IsPwrHibernateAllowed Lib "Powrprof.dll" Alias "IsPwrHibernateAllowed" () As Integer
b) Check for support
Code:
Dim boolHibernateSupport as Boolean = CBool(IsPwrHibernateAllowed())
2) If boolHibernateSupport is True then
Code:
If Application.SetSuspendState(PowerState.Hibernate, True, False) = False Then
      ' Add ErrorHandling code here                 
End If
You may search the MSDN library for more information about the SetSuspendState Function.

The shutdown -s method mentioned above is simpler. The below mentioned method is more complicated but allows greater control. Also, it does not rely on the Shutdown command.

Alternatively you can use the following method to Shutdown/Restart the PC

1) Declare the function
Code:
Public Declare Function ExitWindowsEx Lib "user32.dll" Alias "ExitWindowEx" (ByVal uFlags As System.UInt32, ByVal dwReason As System.UInt32) As Boolean
2) Declare the Constants (flags) needed.

Code:
Public Enum ShutdownType
        EWX_HYBRID_SHUTDOWN = &H400000
        EWX_LOGOFF = &H0
        EWX_POWEROFF = &H8
        EWX_REBOOT = &H2
        EWX_RESTARTAPPS = &H40
        EWX_SHUTDOWN = &H1
        EWX_FORCE = &H4
        EWX_FORCEIFHUNG = &H10
    End Enum

    Public Enum ShutdownReason
        SHTDN_REASON_MAJOR_APPLICATION = &H40000
        SHTDN_REASON_MAJOR_HARDWARE = &H10000
        SHTDN_REASON_MAJOR_LEGACY_API = &H70000
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM = &H20000
        SHTDN_REASON_MAJOR_OTHER = &H0
        SHTDN_REASON_MAJOR_POWER = &H60000
        SHTDN_REASON_MAJOR_SOFTWARE = &H30000
        SHTDN_REASON_MAJOR_SYSTEM = &H50000
        SHTDN_REASON_MINOR_BLUESCREEN = &HF
        SHTDN_REASON_MINOR_CORDUNPLUGGED = &HB
        SHTDN_REASON_MINOR_DISK = &H7
        SHTDN_REASON_MINOR_ENVIRONMENT = &HC
        SHTDN_REASON_MINOR_HARDWARE_DRIVER = &HD
        SHTDN_REASON_MINOR_HOTFIX = &H11
        SHTDN_REASON_MINOR_HOTFIX_UNINSTALL = &H17
        SHTDN_REASON_MINOR_HUNG = &H5
        SHTDN_REASON_MINOR_INSTALLATION = &H2
        SHTDN_REASON_MINOR_MAINTENANCE = &H1
        SHTDN_REASON_MINOR_MMC = &H19
        SHTDN_REASON_MINOR_NETWORK_CONNECTIVITY = &H14
        SHTDN_REASON_MINOR_NETWORKCARD = &H9
        SHTDN_REASON_MINOR_OTHER = &H0
        SHTDN_REASON_MINOR_OTHERDRIVER = &HE
        SHTDN_REASON_MINOR_POWER_SUPPLY = &HA
        SHTDN_REASON_MINOR_PROCESSOR = &H8
        SHTDN_REASON_MINOR_RECONFIG = &H4
        SHTDN_REASON_MINOR_SECURITY = &H13
        SHTDN_REASON_MINOR_SECURITYFIX = &H12
        SHTDN_REASON_MINOR_SECURITYFIX_UNINSTALL = &H18
        SHTDN_REASON_MINOR_SERVICEPACK = &H10
        SHTDN_REASON_MINOR_TERMSRV = &H20
        SHTDN_REASON_MINOR_UNSTABLE = &H6
        SHTDN_REASON_MINOR_UPGRADE = &H3
        SHTDN_REASON_MINOR_WMI = &H15
        SHTDN_REASON_FLAG_USER_DEFINED = &H40000000
        SHTDN_REASON_FLAG_PLANNED = &H80000000
    End Enum
3) Run the command:
Code:
ExitWindowsEx(ShutdownType.EWX_POWEROFF And ShutdownType.EWX_SHUTDOWN, ShutdownReason.SHTDN_REASON_MAJOR_POWER And ShutdownReason.SHTDN_REASON_FLAG_PLANNED)
Of course, this code allows more control. You can also use the new Hybrid shutdown supported by Windows 8.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Shutdown, Restart, or Log Off your computer using VB.Net"
Thread Thread Starter Forum Replies Last Post
Automatic Restart After Shutdown Gregoryl Windows XP Support 10 12-12-2011 10:46 AM
Mortens ShutDown to Shutdown Computer at Desired Date And Time Soumen Tips & Tweaks 1 19-03-2009 10:32 AM
Windows does not Shutdown or Restart Curt Tips & Tweaks 0 08-11-2008 05:14 PM
Shortcut Keys to Shutdown and Restart your Computer Musrat Tips & Tweaks 1 26-09-2008 04:57 PM
Auto Shutdown and Restart John Windows Server Help 4 21-03-2008 10:36 PM


All times are GMT +5.5. The time now is 03:16 AM.