Reputation: 1646
In windows I can log in and switch of user without logging out. Like that I can have multiple users logged in but only one working. How can I know which of the users is currently working.
Upvotes: 1
Views: 849
Reputation: 1646
Actually I found a very good example of microsoft in c# (Detect the Windows session state ).
They register to the system event "SessionSwitch"
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e){
...
}
it needs a dependency in Win32
using Microsoft.Win32;
I tried and it work very good and relatively easy.
Upvotes: 2
Reputation: 262919
To my knowledge, the .NET framework does not expose to managed code the API required to achieve this. You will have to p/invoke quite a few WINAPI functions and define at least one structure and some enums on the .NET side.
If you're willing to follow that path, you can:
Call WTSEnumerateSessions(), passing WTS_CURRENT_SERVER_HANDLE
in the hServer
argument,
Iterate over the returned WTS_SESSION_INFO structures and locate the one whose State
member has the WTSActive
and WTSConnected
bits set (there should only be one in your case),
Pass the SessionId
member of that structure to WTSQuerySessionInformation(), specifying WTSUserName
in the WTSInfoClass
argument,
Read the user name from the returned buffer,
Use WTSFreeMemory() to free the buffer and the array of WTS_SESSION_INFO
structures.
As you can see, this isn't really trivial. Good luck.
Upvotes: 1