Ниже приведен пример:
using System;
using System.DirectoryServices;
namespace DirectoryServicesSamples
{
class DomainListApp
{
[STAThread]
static void Main(string[] args)
{
DirectoryEntry deRootDSE = new DirectoryEntry("LDAP://RootDSE");
string sRootDomain = "LDAP://" + deRootDSE.Properties["rootDomainNamingContext"].Value.ToString();
DirectoryEntry deRoot = new DirectoryEntry(sRootDomain);
DirectorySearcher dsFindDomains = new DirectorySearcher(deRoot);
dsFindDomains.Filter = "(objectClass=domainDNS)";
dsFindDomains.SearchScope = SearchScope.Subtree;
dsFindDomains.PropertiesToLoad.AddRange(new string[] {"distinguishedName", "name"} );
foreach(SearchResult srDomain in dsFindDomains.FindAll())
{
Console.WriteLine(string.Format("{0} ({1})",
srDomain.Properties["name"][0],
srDomain.Properties["distinguishedName"][0]));
}
Console.ReadLine();
}
}
}