473,498 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ 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 14355
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
5445
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
2457
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
2019
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
2113
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
3282
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
2087
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
1672
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
2712
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
1244
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,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
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...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
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,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.