By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

How to get a list of DNS servers

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
---------------------------------------
Nov 16 '05 #2

P: n/a
PP
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/de...ynametopic.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/artic...alance_dns.htm


---------------------------------------------------------------------------------------------------------
"Brian Brown" wrote:
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.
---------------------------------------

Nov 16 '05 #3

P: n/a
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/...etAdaptersInfo (in VB.net
however)

"Peter Gloor" <p_*****@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #4

P: n/a
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(DNSI P.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; }
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Peter Gloor" <p_*****@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
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


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.