Click here to Skip to main content
11,932,677 members (61,363 online)
Rate this:
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 21:18pm
Edited 13-Jan-13 22:54pm

1 solution

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

Solution 1

M_Mogharrabi 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 14-Jan-13 4:10am
I think its correct, for more reference visit : Link
M_Mogharrabi 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 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 14-Jan-13 4:50am
Thanks a lot for your time...
Suvabrata Roy 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.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