.NET Detecting a Logon event in Windows 10

扶醉桌前 提交于 2021-01-28 07:52:55

问题


I've been getting a little stumped on this issue. I have a need to detect when a user logs on, when the Display is Locked, and when the user logs off. In the past I've used a VB.NET application which uses a COM reference to Sens ( System Event Notification Service ) which makes use of the Sens Events ISensLogon interface.

However, since moving to windows 10, I'm no longer able to add Sens as a COM object to my project. Is there an SDK that I'm missing in order to make use of this functionality?

Is there another COM library or reference that I need to use in order to detect these events in Windows 10?


回答1:


Try this:

Imports Microsoft.Win32

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler SystemEvents.SessionSwitch, AddressOf SystemEvents_SessionSwitch
End Sub

Private Sub SystemEvents_SessionSwitch(sender As Object, e As SessionSwitchEventArgs)

    Select Case e.Reason
        Case SessionSwitchReason.SessionLock
            MessageBox.Show("Lock")
        Case SessionSwitchReason.SessionLogoff
            MessageBox.Show("LogOff")
        Case SessionSwitchReason.SessionLogon
            MessageBox.Show("Login")
        Case SessionSwitchReason.SessionUnlock
            MessageBox.Show("Unlock")
        Case Else
    End Select

End Sub

End Class

Try to stay out of COM when and where the .NET framework makes that possible.



来源:https://stackoverflow.com/questions/44010424/net-detecting-a-logon-event-in-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!