Solved

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

Posted on 2006-11-14
4
253 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now