Tuesday, July 5, 2011

Get the User Names from Active Directory using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.AccountManagement;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry domain = new DirectoryEntry("LDAP://domainname.com/CN=Users,DC=domainname,DC=com");

            foreach (DirectoryEntry child in domain.Children)
            {
                Console.WriteLine(child.Name);
            }
            Console.WriteLine("----------------------------------------------------------");

            DomainCollection domains = Forest.GetCurrentForest().Domains;
            foreach (Domain domain1 in domains)
            {
                Console.WriteLine(domain1.Name);
            }
            Console.WriteLine("----------------------------------------------------------");
            DirectoryEntry domain2 = new DirectoryEntry("LDAP://domainname.com/CN=Computers,DC=domainname,DC=com");
            foreach (DirectoryEntry child1 in domain2.Children)
            {
                Console.WriteLine(child1.Name);
            }
            Console.ReadKey();
        } 
    }
}

No comments:

Post a Comment