DNS Nameserver Lookup

M

Magnus R

I am using the System.Net namespace to do DNS lookups of a host and return
their IP address. However I haven't figured out how to use the namespace to
take a domain name and find the responsible nameservers. Does anyone have an
idea how to do this?
 
P

PlatinumBay

Magnus R,

You have a couple options.

You could use PInvoke for the following signature:
Private Declare Function NetGetDCName Lib "netapi32.dll" (ByVal
strServerName As Object, ByVal strDomainName As Object, ByVal pBuffer As
Long) As Long


You could use the System.Net namespace:

Dim MYIP As System.Net.IPHostEntry =
System.Net.Dns.GetHostEntry(My.Computer.Name)
Dim IPaddress As String = MYIP.AddressList.GetValue(0).ToString
Console.WriteLine(System.Net.Dns.GetHostEntry(IPaddress).HostName)


You could also use the System.Net.NetworkInformation namespace for the long
way around, but more detail. Check out this code:

Imports System.Net
Imports System.Net.NetworkInformation

Public Class NetworkInfo
Public Shared Sub ShowIPAddresses()
Dim computerProperties As IPGlobalProperties =
IPGlobalProperties.GetIPGlobalProperties()
Dim nics() As NetworkInterface =
NetworkInterface.GetAllNetworkInterfaces()
Console.WriteLine("Interface information for {0}.{1} ", _
computerProperties.HostName, computerProperties.DomainName)
If (nics Is Nothing OrElse nics.Length < 1) Then
Console.WriteLine(" No network interfaces found.")
Return
End If

Console.WriteLine(" Number of interfaces .................... :
{0}", nics.Length)
For Each adapter As NetworkInterface In nics
Dim adapterProperties As IPInterfaceProperties =
adapter.GetIPProperties()
Dim dnsServers As IPAddressCollection =
adapterProperties.DnsAddresses

If (Not dnsServers Is Nothing) Then
For Each dns As IPAddress In dnsServers
Console.WriteLine(" DNS Servers
.............................. : {0}", dns.ToString())
Console.WriteLine(" Server Name
........................... : {0}",
System.Net.Dns.GetHostEntry(dns.ToString).HostName)
Next
End If
Next
End Sub
End Class


You could also use the registry:
http://www.java2s.com/Code/CSharp/Network/FindDNSServers.htm

Hope this helps,


Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top