I'm trying to monitor the activity of the current user on a computer.
I have a process which monitors what it needs to monitor and saved on a file which user did the activity.
Currently, I'm saving on a file the name of the user who owns the process at the startup of that process, and the process starts automatically when a new user logs on. Thus, I can handle different users logging in on the same machine.
The problem occurs when there are 2 or more logged in users and they are being switched without logging off (for now I'm ignoring the case of RDP logins).
I've tried using the SystemEvents.SessionSwitch event, but it doesn't give me the new active user, it only tells me which user is no longer active. I thought of having a management process that is responsible for killing and restarting the monitor process after a user is switched, but that process also doesn't know which user is currently active.
If anyone has any idea how can I keep track of the active users, any help would be appreciated.
Thanks
It was solved in this thread: How do I get the currently-logged username from a Windows service in .NET?. Tapas had the correct answer.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();