views:

58

answers:

1

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  A: 

using System.DirectoryServices.ActiveDirectory;

....

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

ho1
just perfect.... thanks/