Directory Services

Setting Search Filters

Search filters enable filtering for specific objects by searching for objects based on attributes associated to the object, such as the objectClass. To do this, set up a filter string with the desired attributes included in the string. For wildcard searches, you can enter any part of the string you wish to search on along with a *, such as (anr=test*). When setting up search filters to use with System.DirectoryServices, follow the syntax rules for LDAP filters. To indicate that a filter is used, use the DirectorySearcher property Filter.

For more information about search filters, see Creating a Query Filter.

The following code example shows how to add a search filter.

[C#]
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(anr=test*))";
SearchResultCollection ResEnt = mySearcher.FindAll();
{
				// Handle results.
}	 
// Handle exceptions.