Querying Active Directory in C#

I need to be able to query Active Directory for information about a particular workstation. How does one query Active Directory in this way in C#


Answer this question

Querying Active Directory in C#

  • Ty Y

    Take a look at System.DirectoryServices namespace. A good starting point is:

    http://msdn2.microsoft.com/en-us/library/system.directoryservices.aspx

    If you already know the ADsPath, you can use DirectoryEntry directly, for instance to retrieve the root:

    DirectoryEntry entry = new DirectoryEntry("LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com");
     
    If not, you can use DirectorySearcher to query for it. Check out the docs for examples on how to call it.

    N.B. You'll need to Add Reference... to System.DirectoryServices and add a using directive for the namespace.

    Hopefully this is enough to get you started. If you have a more specific problem, feel free to post it.



  • Marty01

    Thank you so much for this post. I spent a couple hours trying to figure out why my using System..DirectoryServices was not compiling. the add reference was the key.



  • Querying Active Directory in C#