How to get a list of DNS servers

  • Thread starter Thread starter Peter Gloor
  • Start date Start date
P

Peter Gloor

I'm writing an application that requires some dns queries (MX etc) not
provided by System.Net.Dns. So far I managed to write my own dns class with
methods to query a given dns server.

There is only one thing left I could not figure out so far. How can I get
the list of dns servers used by my computer? The code must work under any
Windows version with .NET framework support.

Any ideas are welcome.

Peter
 
Hi Peter,

One way (may not be the "best" way) is to use the System.Diagnostics.Process
class and execute a ipconfig /all, capture the contents and then parse
through to find the DNS servers.

You may also be able to get the list through WMI by querying the
Win32_NetworkAdapterConfiguration class (specifically the
DNSServerSearchOrder) . I have not had a chance to test this method but I
thought it was worth a mention!

I hope this helps.
---------------------------------------
 
Try this one (sorry it is in VB, you might have to change it to c#)
-------------------------------------
Dim MYIP As System.Net.IPHostEntry =
System.Net.Dns.GetHostByName(Server.MachineName)
Dim IPaddress As String
IPaddress = (MYIP.AddressList.GetValue(0).ToString)
----------------------------------------------------------------------------
Dns.GetHostByName Method - Getting IP Address based on DNS name

http://msdn.microsoft.com/library/d.../frlrfsystemnetdnsclassgethostbynametopic.asp

Multiple IPAddress for a DNS name will be seen for DNS round-robin
load-balancing.
The DNS server will return a different address from the list each time
you query it,
so incoming requests will be approximately evenly spread between however
many servers you have. Saves you having to do complicated things with ISA
server or whatever to farm out the incoming requests at your end.

http://content.websitegear.com/article/load_balance_dns.htm
 
PP's suggestion is giving you information on the wrong thing.

Brian's first suggestion is hopelessly klugy and depends on ipconfig being
on the targt machine & along the PATH.

Brain's second suggestion (Win32_NetworkAdapterConfiguration) is probably
the best.

The "official" way to to it (in Win32) is to call GetAdaptersInfo() to get a
list of the NIC cards on the target PC, and then call GetPerAdapterInfo() on
each of them to get the list of DNS servers. www.pinvoke.net offers some
help on call GetAdaptersInfo from a .Net program,
http://www.pinvoke.net/default.aspx/iphlpapi.GetAdaptersInfo (in VB.net
however)
 
I wrote this some time ago:
/// <summary>
/// Summary description for IPConfig.
/// </summary>
public class IPConfig
{
private string hostName;
private string domainName;
private IPAddress[] dnsServers;

public IPConfig()
{
GetParms();
}

private void GetParms()
{
uint uintBufferSize = 0;
ArrayList dnsIPList = new ArrayList();
IPAddress dnsIP;
Native.IP_ADDR_STRING DNSIP;

//run the method once to find the size of the buffer required
if( Native.GetNetworkParams(IntPtr.Zero , ref uintBufferSize) != 111 )
throw new ApplicationException("Error calling GetNetworkParams().");

//declare a space in unmanaged memory to hold the data
IntPtr pBuffer = Marshal.AllocHGlobal((int)uintBufferSize);

//run the function
if( Native.GetNetworkParams( pBuffer, ref uintBufferSize ) !=0 )
throw new ApplicationException("Error getting adapter info.");

Native.FIXED_INFO FInfo =
(Native.FIXED_INFO)Marshal.PtrToStructure(pBuffer,
typeof(Native.FIXED_INFO));
this.hostName = FInfo.HostName;
this.domainName = FInfo.DomainName;

//Get DNS Server IPs:
DNSIP = FInfo.DnsServerList;

dnsIP = GetIP(DNSIP.IpAddress.AddrString);
if ( dnsIP != null )
dnsIPList.Add(dnsIP);

while( DNSIP.Next != IntPtr.Zero)
{
DNSIP = (Native.IP_ADDR_STRING)Marshal.PtrToStructure(DNSIP.Next,
typeof(Native.IP_ADDR_STRING));
dnsIP = GetIP(DNSIP.IpAddress.AddrString);
if ( dnsIP != null )
dnsIPList.Add(dnsIP);
else
break;
}

this.dnsServers = (IPAddress[])dnsIPList.ToArray(typeof(IPAddress));
Marshal.FreeHGlobal(pBuffer);
}

private IPAddress GetIP(string ipString)
{
if ( ipString == null || ipString == "" )
return null;

try
{
return IPAddress.Parse(ipString);
}
catch
{
return null;
}
}

public string HostName
{
get { return this.hostName; }
}

public string DomainName
{
get { return this.domainName; }
}

public IPAddress[] DnsServers
{
get { return this.dnsServers; }
}
}
 
Back
Top