473,785 Members | 2,395 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 14403
Hi Peter,

One way (may not be the "best" way) is to use the System.Diagnost ics.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_NetworkAd apterConfigurat ion class (specifically the
DNSServerSearch Order) . 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.IPHo stEntry =
System.Net.Dns. GetHostByName(S erver.MachineNa me)
Dim IPaddress As String
IPaddress = (MYIP.AddressLi st.GetValue(0). ToString)
----------------------------------------------------------------------------
Dns.GetHostByNa me 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.Diagnost ics.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_NetworkAd apterConfigurat ion class (specifically the
DNSServerSearch Order) . 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_NetworkA dapterConfigura tion) 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 GetPerAdapterIn fo() 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_*****@hotmai l.com> wrote in message
news:eJ******** ******@TK2MSFTN GP09.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.GetNetwo rkParams(IntPtr .Zero , ref uintBufferSize) != 111 )
throw new ApplicationExce ption("Error calling GetNetworkParam s().");

//declare a space in unmanaged memory to hold the data
IntPtr pBuffer = Marshal.AllocHG lobal((int)uint BufferSize);

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

Native.FIXED_IN FO FInfo =
(Native.FIXED_I NFO)Marshal.Ptr ToStructure(pBu ffer,
typeof(Native.F IXED_INFO));
this.hostName = FInfo.HostName;
this.domainName = FInfo.DomainNam e;

//Get DNS Server IPs:
DNSIP = FInfo.DnsServer List;

dnsIP = GetIP(DNSIP.IpA ddress.AddrStri ng);
if ( dnsIP != null )
dnsIPList.Add(d nsIP);

while( DNSIP.Next != IntPtr.Zero)
{
DNSIP = (Native.IP_ADDR _STRING)Marshal .PtrToStructure (DNSIP.Next,
typeof(Native.I P_ADDR_STRING)) ;
dnsIP = GetIP(DNSIP.IpA ddress.AddrStri ng);
if ( dnsIP != null )
dnsIPList.Add(d nsIP);
else
break;
}

this.dnsServers = (IPAddress[])dnsIPList.ToAr ray(typeof(IPAd dress));
Marshal.FreeHGl obal(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_*****@hotmai l.com> wrote in message
news:eJ******** ******@TK2MSFTN GP09.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
5475
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 in the network like this C# code: public static string GetAvailableSQLServers() { // declare arraylist to hold results ArrayList servers = new ArrayList();
1
2480
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 what to do in order to obtain list of all such servers?! When you open Sql Service Manager (for sql server2000) you've got such list. Thanks,
7
2048
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
2146
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 already ON usenet to follow proper usenet guidelines. In order to be under the comp.* hierarchy the server MUST go through RFD and CFV. Otherwise it is a ROGUE group!
3
3313
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 question can be solved, is it also possible to manage the DHCP servers (list, add, remove scopes, ...) ? Thank you.
1
2107
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. Instead, I just have a warning for security reasons and 2 buttons (Attach | Don't attach). Let me explain a bit... I have several web applications running on different servers. All servers are XP 2003 Servers with IIS 6. My applications runs in...
3
1695
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 clearcase views are on each server and then only choose the one that has the least number of views that given day. This is what I have so far. my @servers = qw ( srv30bld4 srv30bld5 srv40bld2 srv40bld3 srv40bld4
2
2734
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 http://www.csharphelp.com/archives2/archive342.html SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass(); SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers(); for (int i = 0; i < sqlServers.Count; i++) ...
7
1257
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, please forgive my attrocious naming conventions. Basically i'm trying to spin up some subprocesses that will ping a group of servers and then wait until all of them have completed (good or bad), store the ping result and the return code, and...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6743
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.