tags:

views:

30

answers:

2

Using WMI (regardless of the actual programming language), it's possible to get the currently logged on user using a query such as Select UserName from Win32_ComputerSystem. All nice and good.

But what if multiple user sessions and/or Terminal Services are involved? Is it possible to list all the currently logged on users on a given system? How?

If WMI is not the best option here, and/or if there are other available tools, feel free to advice. I'm mainly interested in .NET/C#.

A: 

What you want is WTSEnumerateSessions, however this will involve a p/invoke call. If you want a nice c# wrapper library I recommend cassia. As for a purely WMI solution I do not know.

Found a MSDN post about some WMI calls that are similar to what you want.

Scott Chamberlain
This looks quite RDP-specific; would it list console sessions, too? Also, I need to do this remotely, which WMI fits quite nicely.
Massimo
You can only have one active console session at a time with WinNT, but yes it works with console sessions.
Scott Chamberlain
Also at least you now have a starting point see if there is a WMI interface to WTSEnumerateSessions
Scott Chamberlain
+1  A: 

This answer already exists on net. Did you try Google? I did and found (all WMI related) -

  1. StackOverflow question (this makes your question a duplicate)

  2. MSDN blog entry - worth trying

  3. Web search result

I strongly recommend using a search engine first - stackoverflow/web - and if you still can't find answers, go ahead and post your question!

Nayan
That's exactly what I did. I searched a lot, and this is how I found the WMI query I quoted in the question; I also had already stumbled upon some of your links, like the third one (which uses exactly the same query, and so only shows one user). Others seem more promising, mainly the SO one. Thanks for your answer, but please try to not be so rude.
Massimo
Massimo, I wasn't being rude, but I'm aware that my direct answer might appear so. :) I was trying to show how to usually go about finding a solution.
Nayan