Home
Products
Forums
Help
Publish Article

How to get list of domains in Active Directory

Contiuing from our previous artcile How to get list of members in a group, we will show you some code on how to get of domains in an Active Directory tree using DirectoryServices classes in Microsoft .Net framework.

private void Page_Load(object sender, System.EventArgs e)
{
	StringCollection adDomains = this.GetDomainList();
	foreach (string strDomain in adDomains)
	{
		Response.Write("<br>" + strDomain + "</b>");
	}
}

private StringCollection GetDomainList()
{
	StringCollection domainList = new StringCollection();
	try
	{
		DirectoryEntry en = new DirectoryEntry("LDAP://");
		// Search for objectCategory type "Domain"
		DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
		SearchResultCollection coll = srch.FindAll();
		// Enumerate over each returned domain.
		foreach (SearchResult rs in coll)
		{
			ResultPropertyCollection resultPropColl = rs.Properties;
			foreach( object domainName in resultPropColl["name"])
			{
				domainList.Add(domainName.ToString());
			}
		}
	}
	catch (Exception ex)
	{
		Trace.Write(ex.Message);
	}
	return domainList;
}							
							
The above code has been tested on the following platforms.
  • Windows 2000 Advanced Server
  • Windows 2003 Enterprise Server (RTM)
Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix