I saw a question in a forum asking how to enumerate computers available in a domain. Though it might seem little difficult, it can be accomplished in less than 10 line of code. Thought would make it as a post for everyone’s benefit. Here we go:

usingSystem.DirectoryServices;

public void PrintComputersInDomain (string domainName)
{
    DirectoryEntry de = new DirectoryEntry ("LDAP://" + domainName);
    de.Children.SchemaFilter.Add ("computer");
    foreach (DirectoryEntry c in de.Children)
    {
        Console.WriteLine (c.Name);
    }
}

Please note when you try this code, it might take seconds to finish if there are hundreds of computers in the given domain (of course, you can always do “kill process” J).

Yesterday, I came across a question in one of the .NET newsgroups asking how to get the IP address of the DNS servers available in the network. Though there is no direct way of getting this information (yet, at least to my knowledge) via .NET 2.0 class library, there is at least one indirect way:


For a change, the code snippet is in Visual Basic .NET :-)


Imports System.Net.NetworkInformation
Imports System.Net

Dim nics As NetworkInterface()
Dim dnsIPs As IPAddressCollection

nics = NetworkInterface.GetAllNetworkInterfaces()

For Each nic As NetworkInterface In nics
If (nic.OperationalStatus = OperationalStatus.Up) Then
dnsIPs = nic.GetIPProperties().DnsAddresses
For Each dnsIp As IPAddress In dnsIPs
MessageBox.Show(dnsIp.ToString())
Next
End If
Next


Checking for operational status is not required unless you want to loop through inactive network interfaces as well.