Setting Search Filters
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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 the topic Creating a Query Filter in the MSDN Library at For more information about the objectClass attribute, see the topic objectClass in the MSDN Library at

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

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.
© 2016 Microsoft