get all domain names on network
        Posted  
        
            by user175084
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user175084
        
        
        
        Published on 2010-04-13T22:54:32Z
        Indexed on 
            2010/04/14
            8:13 UTC
        
        
        Read the original article
        Hit count: 382
        
i need to get the list of domain names on my network...
but i am only getting the domain name with which i log into... so for example there are 2 domains "xyz" and "xyz2" but i get only the domain with which i log into....
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;
    }               
        © Stack Overflow or respective owner