Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

How to make a .NET Windows apllication to detect Logon, Logoff and Switch User events?
could you please provide me the code.

Thans,
Ram

Follow-up comment from the OP:
VB
Private Sub SystemEvents_SessionSwitch(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
 Try
  Select Case e.Reason
    Case Microsoft.Win32.SessionSwitchReason.SessionLock
      ClientStatus(MBSMessage.ClientStatus.Locked)
      UserLockUnlockSettingUPdate(MBSMessage.ClientStatus.Locked)
      timerMessageRecurrance.Stop()
      Exit Select
    Case Microsoft.Win32.SessionSwitchReason.SessionUnlock, Microsoft.Win32.SessionSwitchReason.ConsoleConnect, Microsoft.Win32.SessionSwitchReason.RemoteConnect
      timerMessageRecurrance.Start()
      ClientStatus(MBSMessage.ClientStatus.Login)
      UserLockUnlockSettingUPdate(MBSMessage.ClientStatus.Login)
      Exit Select
    Case Microsoft.Win32.SessionSwitchReason.SessionLogoff
      ClientStatus(MBSMessage.ClientStatus.LogOff)
      timerMessageRecurrance.Stop()
      Exit Select
    'added this logon case for DISPLAY_ALERT_DELAY_DURATION feature to work
    Case Microsoft.Win32.SessionSwitchReason.SessionLogon
      timerMessageRecurrance.Start()
      ClientStatus(MBSMessage.ClientStatus.Login)
      Exit Select
    Case Microsoft.Win32.SessionSwitchReason.RemoteDisconnect
      ClientStatus(MBSMessage.ClientStatus.LogOff) timerMessageRecurrance.Stop()
      Exit Select
  End Select
Posted
Updated 25-Aug-11 10:56am
v3
Comments
Herman<T>.Instance 25-Aug-11 4:18am    
what have you tried?
[no name] 25-Aug-11 4:55am    
He already tried to find somebody to write it for him.
rambuzz 25-Aug-11 5:00am    
I already tried the following :

Private Sub SystemEvents_SessionSwitch(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
Try
Select Case e.Reason
Case Microsoft.Win32.SessionSwitchReason.SessionLock
ClientStatus(MBSMessage.ClientStatus.Locked)
UserLockUnlockSettingUPdate(MBSMessage.ClientStatus.Locked)
timerMessageRecurrance.Stop()
Exit Select
Case Microsoft.Win32.SessionSwitchReason.SessionUnlock, Microsoft.Win32.SessionSwitchReason.ConsoleConnect, Microsoft.Win32.SessionSwitchReason.RemoteConnect
timerMessageRecurrance.Start()
ClientStatus(MBSMessage.ClientStatus.Login)
UserLockUnlockSettingUPdate(MBSMessage.ClientStatus.Login)
Exit Select
Case Microsoft.Win32.SessionSwitchReason.SessionLogoff
ClientStatus(MBSMessage.ClientStatus.LogOff)
timerMessageRecurrance.Stop()
Exit Select
'added this logon case for DISPLAY_ALERT_DELAY_DURATION feature to work
Case Microsoft.Win32.SessionSwitchReason.SessionLogon
timerMessageRecurrance.Start()
ClientStatus(MBSMessage.ClientStatus.Login)
Exit Select
Case Microsoft.Win32.SessionSwitchReason.RemoteDisconnect
ClientStatus(MBSMessage.ClientStatus.LogOff)
timerMessageRecurrance.Stop()
Exit Select
End Select
[no name] 25-Aug-11 5:18am    
Yuck, that's obviously the other C#, known as VB :)

That's nice and well. You can handle SessionSwitchEvents. But without some component ever raising the event this will do nothing.
rambuzz 25-Aug-11 5:22am    
every session is raising but logon event is not raising, that is what bothering me.
please do let me know if any other way is der to raise logon event.

Thanks,
Ram

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900