How to get a list of all domains?
Posted
by AngryHacker
on Stack Overflow
See other posts from Stack Overflow
or by AngryHacker
Published on 2010-04-07T22:41:26Z
Indexed on
2010/04/07
22:43 UTC
Read the original article
Hit count: 325
I'm trying to get all domains that are available in the Windows Login dialog (in the Domain dropdown).
I've tried the following code but it only returns the domain I am logged into. Am I missing something?
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;
© Stack Overflow or respective owner