Log Off/ Shutdown / Restart / Hibernate Class

This snippet is a class that contains various subroutines to control how your computer shuts down, log's off, etc.

Instructions

  1. Create a new class, call it Shutdown.vb
  2. Copy and paste the following code into the class
  3. See the example on how you can call the subroutines from the class in your main form.

[VB.NET]

Public Class Shutdown

    ''' <summary>
    ''' Shutsdown a computer.
    ''' </summary>
    ''' <parameters>
    ''' OptSwitches - Optional Switches you would like to add to the statement, e.g
    '''                      "-m //computername". Type "shutdown -?" into cmd for more info.
    ''' </parameters>
    Public Shared Sub ShutdownComputer(Optional ByRef OptSwitches As String = "")
        System.Diagnostics.Process.Start("shutdown", "-s" + OptSwitches)
    End Sub

    ''' <summary>
    ''' Logs off the computer.
    ''' </summary>
    ''' <parameters>
    ''' OptSwitches - Optional Switches you would like to add to the statement, e.g
    '''                      "-m //computername". Type "shutdown -?" into cmd for more info.
    ''' </parameters>
    Public Shared Sub LogoffComputer(Optional ByRef OptSwitches As String = "")
        System.Diagnostics.Process.Start("shutdown", "-l" + OptSwitches)
    End Sub

    ''' <summary>
    ''' Restarts a computer.
    ''' </summary>
    ''' <parameters>
    ''' OptSwitches - Optional Switches you would like to add to the statement, e.g
    '''                      "-m //computername". Type "shutdown -?" into cmd for more info.
    ''' </parameters>
    Public Shared Sub RestartComputer(Optional ByRef OptSwitches As String = "")
        System.Diagnostics.Process.Start("shutdown", "-r" + OptSwitches)
    End Sub

    ''' <summary>
    ''' Aborts a shutdown (only aborts shutdowns with a countdown (timeout).).
    ''' </summary>
    ''' <parameters>
    ''' OptSwitches - Optional Switches you would like to add to the statement, e.g
    '''                      "-m //computername". Type "shutdown -?" into cmd for more info.
    ''' </parameters>
    Public Shared Sub AbortShutdown(Optional ByRef OptSwitches As String = "")
        System.Diagnostics.Process.Start("shutdown", "-a" + OptSwitches)
    End Sub

    ''' <summary>
    ''' Hibernates a computer.
    ''' </summary>
    ''' <parameters>
    ''' OptSwitches - Optional Switches you would like to add to the statement, e.g
    '''                      "-m //computername". Type "shutdown -?" into cmd for more info.
    ''' </parameters>
    Public Shared Sub HibernateComputer(Optional ByRef OptSwitches As String = "")
        System.Diagnostics.Process.Start("shutdown", "-h" + OptSwitches)
    End Sub

End Class

Examples:

As the subroutines are public, and shared throughout the solution, you are able to call it in other classes (e.g. the main form).

Shutdown.AbortShutdown()

If you would like to add some additional switches to it (the parameter is optional), you can always add them like this:

Shutdown.ShutdownComputer("-t 15")
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License