Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can i get all current usernames of a windows domain?

if (!String.IsNullOrEmpty(domainName))
   {
      DirectoryEntry myDirectoryEntry = new DirectoryEntry(String.Format("LDAP://{0}", domainName));
    DirectorySearcher mySearcher = new DirectorySearcher(myDirectoryEntry);
    SortOption mySort = new SortOption("sn", SortDirection.Ascending);
    mySearcher.Filter = ("(objectCategory=person)");
    mySearcher.Sort = mySort;
      foreach (SearchResult resEnt in mySearcher.FindAll())
        {
         
          if ( System.Text.RegularExpressions.Regex.IsMatch(resEnt.Properties["displayName"][0].ToString(), "         |admin|test|service|system|[$]", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
          {
           int space = resEnt.Properties["displayName"][0].ToString().IndexOf(" ");
           string formattedName = String.Format("{0}",
           resEnt.Properties["displayName"][0].ToString().Substring(space).PadRight(25), );
                  
           userList.Add(formattedName);
                            
           }
                       
           }
                        
     }




i tried this code but userList gets all registered Usernames in the Domain. I want to filter current logged in users
Posted
Updated 5-May-11 21:57pm
v2

1 solution

Look at Howto: (Almost) Everything In Active Directory via C#[^], perhaps you can find some good answers there.
 
Share this answer
 
Comments
Sandeep Mewara 7-May-11 14:43pm    
Good link. 5!
Kim Togo 9-May-11 3:06am    
Thanks

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