c# - How to get Notification when "switch User" happen in window7 -
i building window application in c# notify me when switch user happen. right m using "sessionswitch" event notification.
private void startlisning() { microsoft.win32.systemevents.sessionswitch +=new microsoft.win32.sessionswitcheventhandler(systemevents_sessionswitch); this.eventhandlercreated = true; } private void systemevents_sessionswitch(object sender, eventargs e) { system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "switchuser\t" + datetime.now.tostring() + environment.newline); }
but in case sending notification when "unlock" happen . don't want. need when user switch user in system application should notification , log log file. should log when lock or switchuser happen.
thanks in advance.
private void form1_load(object sender, eventargs e) { startlisning(); } private bool eventhandlercreated; private void startlisning() { microsoft.win32.systemevents.sessionswitch +=new microsoft.win32.sessionswitcheventhandler(systemevents_sessionswitch); systemevents.sessionswitch += new sessionswitcheventhandler(systemevents_sessionswitch1); this.eventhandlercreated = true; } private void systemevents_sessionswitch1(object sender, sessionswitcheventargs e) { switch (e.reason) { case sessionswitchreason.sessionlock: system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "lock\t" + datetime.now.tostring() + environment.newline); break; case sessionswitchreason.sessionlogon: system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "login\t" + datetime.now.tostring() + environment.newline); break; case sessionswitchreason.sessionunlock: system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "unlock\t" + datetime.now.tostring() + environment.newline); break; case sessionswitchreason.consoleconnect: system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "unlockafetswitchuser\t" + datetime.now.tostring() + environment.newline); break; case sessionswitchreason.consoledisconnect: system.io.file.appendalltext(path.combine(environment.getfolderpath(environment.specialfolder.applicationdata), "testtest.txt"), "switchuser\t" + datetime.now.tostring() + environment.newline); break; } }
this notify when "switch user" happen , notify when "user resume" . 'sessionswitchreason.consoledisconnect' event fire when switch user happen . code tested in windows7 , working fine.
this code create "testtest.txt" log file in appdata folder. u can reach there using window+r , search '%appdata%'.
it log notification below : 1. login datetime 2. lock datetime 3. unlock datetime 4. switch user datetime 5. switch user resume datetime
Comments
Post a Comment