using System.DirectoryServices; //... string searchFilter = "(objectClass=user)"; //search for all user objects DirectoryEntry entry = new DirectoryEntry("LDAP://YourDomain"); DirectorySearcher searcher = new DirectorySearcher(entry); searcher.Filter = searchFilter; SearchResultCollection results = searcher.FindAll(); foreach (SearchResult result in results) { Console.WriteLine("Name: " + result.Properties["name"][0].ToString()); Console.WriteLine("Path: " + result.Path); }
using System.DirectoryServices; //... string searchFilter = "(&(objectClass=person)(objectCategory=user)(samaccountname=testuser))"; DirectoryEntry entry = new DirectoryEntry("LDAP://YourDomain"); DirectorySearcher searcher = new DirectorySearcher(entry); searcher.Filter = searchFilter; SearchResult result = searcher.FindOne(); if (result != null) { Console.WriteLine("Name: " + result.Properties["name"][0].ToString()); Console.WriteLine("Path: " + result.Path); }This example searches for a specific user object with the samaccountname "testuser" and outputs the name and path. The DirectorySearcher class is part of the System.DirectoryServices.dll library which is included in the .NET Framework.