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 https://go.microsoft.com/fwlink/?LinkID=27252. For more information about the objectClass attribute, see the topic objectClass in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.
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.
See Also
Reference
System.DirectoryServices
DirectorySearcher
Concepts
Send comments about this topic to Microsoft.
Copyright © 2007 by Microsoft Corporation. All rights reserved.