get all domain names on network

↘锁芯ラ 提交于 2019-12-24 08:46:44

问题


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;
    }               

回答1:


using System.DirectoryServices.ActiveDirectory;  

....

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


来源:https://stackoverflow.com/questions/2633751/get-all-domain-names-on-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!