Night | Snow

How to get all domains in the Active Directory using C#

Posted on: December 17th, 2009

The following C# function returns a list of all domains in the Active Directory using System.DirectoryServices.

private StringCollection GetDomains()
{
    StringCollection domains= new StringCollection();
    try
    {
        DirectorySearcher ds= new DirectorySearcher("objectCategory=Domain");
        SearchResultCollection src= ds.FindAll();
        foreach (SearchResult rs in src)
        {
            ResultPropertyCollection rpc= rs.Properties;
            foreach( object d in rpc["name"])
            {
                domains.Add(d.ToString());
            }
        }
    }
    catch (Exception)
    {
    }
    return domains;
}

News

Martin Fischer said:

Client and partner of simra.ch

Wir können Simon Schärer als kompetenter und zuverlässiger Programmierer höchst weiterempfehlen. Er ist nicht nur ein guter Programmierer, sondern auch ein ausgezeichneter Designer.
Danke Developer’s Island

Follow us on Twitter

favorite

I get asked a lot - which SDK should we use for web programming? I recommend Netbeans

my toolbar

Simra GameMP3 playersubscribe to RSSMy delicious bookmarksFacebookTwitterE-mail

No Responses to “How to get all domains in the Active Directory using C#”

RSS Feed Icon  Subscribe to comments follow-up

Trackbacks/Pingbacks

Leave a reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest Tweet

loading...