Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C#
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
Updated 13-Jan-13 21:54pm
v2

1 solution

 
Share this answer
 
Comments
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 x.y.com","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 "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 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...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900