Create a new DLL project. Add a new class to it and add a static method that returns an
IEnumerable<string>
. Or have it fire an event which will have the string as an arg - that way you don't have to wait for the entire search to complete before you can start processing the results.
Here's the former approach:
public static IEnumerable<object> GetSearchResults()
{
Collection<object> results = new Collection<object>();
DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
foreach (SearchResult result in searcher.FindAll())
{
var value = result.GetDirectoryEntry().Properties["sAMAccountName"].Value;
results.Add(value);
}
return results;
}
And here's the other option I mentioned:
public event EventHandler<SearchEventArgs> ItemFound;
public void StartSearch()
{
DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
foreach (SearchResult result in searcher.FindAll())
{
var value = result.GetDirectoryEntry().Properties["sAMAccountName"].Value;
if (ItemFound != null)
{
ItemFound(this, new SearchEventArgs() { Value = value });
}
}
}
where
SearchEventArgs
is:
public class SearchEventArgs : EventArgs
{
public object Value { get; set; }
}