Solved

Get DNS servers (name servers) that is assigned to the machine

Posted on 2006-11-14
4
259 Views
Last Modified: 2010-04-16
Basically, I want to make a DNS query, but first, I need to know which DNS server to use.

The DNS server the machine use is a good choice.

Thanks in advance.
0
Comment
Question by:pmengal
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
redpipe earned 370 total points
ID: 17937484

using System.Net.NetworkInformation;
using System.Net;

public static string[] GetNetworkInterfaces()
    {
      NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
      if (nics == null || nics.Length < 1) {
        throw new Exception("No network interfaces found");
      }

      String[] s = new string[nics.Length];
      int i = 0;
      foreach (NetworkInterface adapter in nics) {
        s[i] = adapter.Description;
        i++;
      }
      return s;
    }

private static string[] GetAdapterIpAdresses(NetworkInterface adapter){
      if (adapter == null) {
        throw new Exception("No network interfaces found");
      }
      IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
      string[] s = null;
      IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
      if (dnsServers != null) {
        s = new string[dnsServers.Count];
        int i = 0;
        foreach (IPAddress dns in dnsServers) {
          s[i] = dns.ToString();
          i++;
        }
      }
      return s;
    }
0
 
LVL 22

Expert Comment

by:cookre
ID: 17937562
Note that IPInterfaceProperties.DnsAddresses is only in .NET 2 and 3.

For pre-2.0, you can either run ipconfig and parse its stdout, or walk some rather tortuous, OS specific Registry chains.
0
 

Author Comment

by:pmengal
ID: 17937639
Brillant!
0
 
LVL 8

Expert Comment

by:redpipe
ID: 17937641
Another common way of doing this is through WMI by querying the Win32_NetworkAdapterConfiguration class. I have no experience with this myself, but seen loads of code on it. An ingenious app written by Omar Al Zabir for switching between different network configurations, and written in C# v.1.1 may help you on your way.

SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places
http://www.codeproject.com/csharp/oazswitchnetconfig.asp
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question