Utility classes to get notified about session state changes on Windows.
using Nefarius.Utilities.SessionNotification;
// subscribe to all sessions (required when run as Windows service)
var sch = new SessionChangeHandler(true);
// session locked event (Win + L)
sch.SessionLock += id =>
{
var username = SessionUtilities.GetUsernameBySessionId(id, false);
Console.WriteLine($"Session ID {id}, user {username} locked");
};
// session unlocked event
sch.SessionUnlock += id =>
{
var username = SessionUtilities.GetUsernameBySessionId(id, false);
Console.WriteLine($"Session ID {id}, user {username} unlocked");
};