473,461 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get a list of DNS servers

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
4 14353
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Oliver Braun | last post by:
I know this is a very common issue and I found a lot of hints on this topic in www but I did not find a very good solution for this task. Most of the solutions use SQLDMO to list all sql servers...
1
by: Piotrek Stachowicz | last post by:
Hi, I'd like to display list of all MS SQL servers which are available on the network (I write application which uses database located on one of the machines in my LAN). Has anyone got any idea...
7
by: EvanK | last post by:
Is there a way to access a list of available sql servers in vb and make a dropdown list?
0
by: Mike Cox | last post by:
Andy M wrote: > ALERT > > There is a person by the name of Mike Cox who's trying > to turn this mailing list into a Big-8 newsgroup. No, I'm trying to get teh postgresql groups which are...
3
by: jmd.msdn | last post by:
Hello. Is it possible to obtain programmatically, with .net 2.0 + C# + Active Directory or P/Invoke, a list of the authorized current DHCP servers in a forest/domain ? And, if the above...
1
by: Martin Simard | last post by:
Hi all, In VS 2003, when I attach to a remote process for debugging, I can see the list of modules loaded by the process before attaching to it. This list is not there anymore in VS 2005....
3
by: joeke3el | last post by:
Hi everyone, I have not touched Perl in the last 4 years, my books are at work and I have something here I'm struggling to figure out. From a known list of servers, I need to gather up how many...
2
by: querry | last post by:
Hi all, I am trying to get a list of all the available sql servers and then populate them in a combo box. I do this with the following code taken from...
7
by: Radamand | last post by:
This has been driving me buggy for 2 days, i need to be able to iterate a list of items until none are left, without regard to which items are removed. I'll put the relevant portions of code below,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.