Click here to Skip to main content
11,804,008 members (77,629 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# domain
Hi all,
I need to get a list of all in-use (not all possible) computer names and their current logged on users on a local network.

I have tried the following code.

PrincipalContext ctx = new  PrincipalContext(ContextType.Domain);
IPAddress[] ip = Dns.GetHostAddresses(ctx.ConnectedServer.Split('.').GetValue(0).ToString());
DirectoryEntry dirEn = new DirectoryEntry(String.Format("LDAP://" + ip[0] + "","DC=x,DC=y, DC=com"));
DirectorySearcher dirSearcher = new DirectorySearcher(dirEn);
SearchResultCollection result;
result = dirSearcher.FindAll();

It returns list of all computers in my network but i want list of only logged on computers(I mean which computers that users are logging in and using them right now).
Posted 13-Jan-13 20:18pm
Edited 13-Jan-13 21:54pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

M_Mogharrabi at 14-Jan-13 4:01am
Hi Suvabrata Roy, and thanks for your reply.I tested that code but it does not work with "winNT:" as the parameter of DirectoryEntry and i tried String.Format("LDAP://MY_SERVER_IP","DC=x,DC=y, DC=com") instead.
Is it correct?
Suvabrata Roy at 14-Jan-13 4:10am
I think its correct, for more reference visit : Link
M_Mogharrabi at 14-Jan-13 4:21am
Thanks again,And the last question(but not the least),
I have found another sample of using cassia in "" but it is a little different:

ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetLocalServer())
foreach (ITerminalServicesSession session in server.GetSessions())
Console.WriteLine("Hi there, " + session.UserAccount + " on session " + session.SessionId);
Console.WriteLine("It looks like you logged on at " + session.LoginTime + " and are now " + session.ConnectionState);

Do you think which one is better?
Suvabrata Roy at 14-Jan-13 4:46am
Both are quite same...

Your main performance is depending on how many session's are their, in both cases you are using Enumeration but in
1st case : you check first and then deliver
2nd case : you are extracting all then do what ever check you need.
but 2nd code is looking more appropriate and structured. :)
M_Mogharrabi at 14-Jan-13 4:50am
Thanks a lot for your time...
Suvabrata Roy at 14-Jan-13 4:54am
you most welcome...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 CPallini 310
2 Sergey Alexandrovich Kryukov 234
3 CHill60 230
4 Suvendu Shekhar Giri 200
0 OriginalGriff 3,140
1 Maciej Los 1,910
2 KrunalRohit 1,907
3 CPallini 1,845
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100