问题
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