Getting the IP Address of the DNS Server(s)

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.