How to get all domains in the Active Directory using C#
Posted on: December 17th, 2009
The following C# function returns a list of all domains in the Active Directory using System.DirectoryServices.
{
StringCollection domains= new StringCollection();
try
{
DirectorySearcher ds= new DirectorySearcher("objectCategory=Domain");
SearchResultCollection src= ds.FindAll();
foreach (SearchResult rs in src)
{
ResultPropertyCollection rpc= rs.Properties;
foreach( object d in rpc["name"])
{
domains.Add(d.ToString());
}
}
}
catch (Exception)
{
}
return domains;
}
Posted in C#,Code Snippets | Trackback Url








No Responses to “How to get all domains in the Active Directory using C#”
Trackbacks/Pingbacks
Leave a reply