vv vv

vv vv

  • NA
  • 1
  • 0

Windows Service that detects logon/logoff events

Jun 27 2009 5:41 AM


Hi,
I want to write a window service that detects the window logon/logoff event. i have written below listed code it is working fine at logoff time but not working at logon time .

protected override void OnStart(string[] args)
{
StartListenLogon();  //Not working when i log on my system
StartListenLogOff();
}
public
void StartListenLogon()
{
string _Query = "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_LogonSession'";
EventWatcher =
new ManagementEventWatcher(_Query);
EventWatcher.EventArrived +=
new EventArrivedEventHandler(EventWatcher_EventArrived);
EventWatcher.Start();
}
void EventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{

string fileName = string.Empty;
fileName =
@"D:\WindowsWatcherOn\" + DateTime.Now.ToString("dd-MMM-yyyy_hh-mm-ss") + "LOGON.txt";
File.Create(fileName);
}
public void StartListenLogOff()
{
SystemEvents.SessionEnded += new SessionEndedEventHandler(SystemEvents_SessionEnded);
}
void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
{
string fileName = string.Empty;
fileName =
@"D:\WindowsWatcherLogOff\" + DateTime.Now.ToString("dd-MMM-yyyy_hh-mm-ss") + "_LOGOFF.txt";
File.Create(fileName);
} 
 
please send me the solution on [email protected]
 

please help thanks in advance  

Vikas Aggarwal