Search code examples
vb.netshutdown

Computer Shutdown event ? VB.NET


Possible Duplicate:
Detecting windows shutdown event

Is there is any event about shutting down in VB.NET ? I want to execute statement when the computer the user clicks shut down , can this be done in vb.net ?


Solution

  • Example from here

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler Microsoft.Win32.SystemEvents.SessionEnding, _
                   AddressOf Handler_SessionEnding
        End Sub
    
        Public Sub Handler_SessionEnding(ByVal sender As Object, _
                   ByVal e As Microsoft.Win32.SessionEndingEventArgs)
            If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then
                MessageBox.Show("User is logging off")
            ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then
                MessageBox.Show("System is shutting down")
            End If
        End Sub
    End Class