Solved

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

Posted on 2006-11-14
4
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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