How to get list of all domain controllers in C#

In this article I am going write C# code examples to get list of  Domain Controllers in Active Directory using C#. We can get Domain Controllers list using C# in different ways. Here I have given three different examples.

Get List of Domain Controllers in C# using Directory Searcher class

     private static void GetListOfDomainControllersByDirectorySearcher()
        {
            try
            {
                DirectoryEntry domainEntry = new DirectoryEntry("LDAP://DC=Work2008,DC=local", "Administrator", "password");
                DirectorySearcher searcher = new DirectorySearcher(domainEntry);

                searcher.Filter = "(&(objectCategory=computer)(objectClass=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))";

                searcher.PropertiesToLoad.AddRange(new string[] { "name", "operatingsystem" });

                foreach (SearchResult result in searcher.FindAll())
                {
                    Console.WriteLine("Name: " + result.Properties["name"][0].ToString());
                    Console.WriteLine("Operating Systme: " + result.Properties["operatingsystem"][0].ToString());
                }
            }
            catch (Exception ex)
            {
        
            }
        }

Get List of Domain Controllers in C# using Domain class

     private static void GetListOfDomainControllers()
        {
            try
            {
                Domain domain = Domain.GetCurrentDomain();

                foreach (DomainController dc in domain.DomainControllers)
                {
                    Console.WriteLine("Name: " + dc.Name);
                    Console.WriteLine("Operating Systme: " + dc.OSVersion);
                    Console.WriteLine("IP Address: " + dc.IPAddress);
                    Console.WriteLine("Site Name: " + dc.SiteName);
                }
            }
            catch (Exception ex)
            {
            }
        }

Get All discoverable Domain Controllers in C# using Domain class

     private static void GetListOfDiscoverableDomainControllers()
        {
            try
            {
                Domain domain = Domain.GetCurrentDomain();

               //Finds all of the discoverable domain controllers in this domain.

                foreach (DomainController dc in domain.FindAllDiscoverableDomainControllers())
                {
                    Console.WriteLine("Name: " + dc.Name);
                    Console.WriteLine("Operating Systme: " + dc.OSVersion);
                    Console.WriteLine("IP Address: " + dc.IPAddress);
                    Console.WriteLine("Site Name: " + dc.SiteName);
                }
            }
            catch (Exception ex)
            {
            }
        }



Note: For better performance use Directory Searcher class method to get list Domain Controllers in C# instead of Domain class.

Related Articles:

– Active Directory Attribute mapping with Friendly name – user
– Active Directory Search Filter Examples
– Create new Active Directory User in C#
– Remote Group Policy update using gpupdate in C#
– Restore a deleted Active Directory object using C#
– Active Directory search filter by ObjectGuid 

Thanks,
Morgan
Software Developer

Advertisement

1 thought on “How to get list of all domain controllers in C#”

Leave a Comment