Get all domain names on the network

I need to get a list of domain names on my network ...

but I only get the domain name with which I log in ... therefore, for example, there are 2 domains "xyz" and "xyz2", but I only get the domain with which I log in ....

here is my code:

if (!IsPostBack) { StringCollection adDomains = this.GetDomainList(); foreach (string strDomain in adDomains) { DropDownList1.Items.Add(strDomain); } } } 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; } 
+1
source share
1 answer
 using System.DirectoryServices.ActiveDirectory; 

....

 Forest currentForest = Forest.GetCurrentForest(); DomainCollection domains = currentForest.Domains; foreach(Domain objDomain in domains) { System.Diagnostics.Debug.WriteLine(objDomain.Name); } 
+1
source

All Articles