Click here to Skip to main content
Rate this: bad
good
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] + " x.y.com","DC=x,DC=y, DC=com"));
DirectorySearcher dirSearcher = new DirectorySearcher(dirEn);
SearchResultCollection result;
 
dirSearcher.Filter="(&(ObjectClass=computer))";
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
v2

1 solution

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

Solution 1

  Permalink  
Comments
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 x.y.com","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 "http://www.findbestopensource.com/product/cassia" but it is a little different:
 
ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetLocalServer())
{
server.Open();
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)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
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