Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

8 Experts available now in Live!

Get 1:1 Help Now