|
To simplify the unwieldy state of computer naming, the Domain Name System (DNS) was created. It allows the master host database to be split up and distributed among multiple systems on the Internet. DNS uses a hierarchical database approach, creating levels of information that can be split and stored on various systems throughout the Internet.
DNS Structure
The structure of a hierarchical database is similar to an organization chart with nodes connected in a treelike manner (that's the hierarchical part). The top node is called the root . The root node does not explicitly show up in Internet host addresses, so it is often referred to as the “nameless” node. Multiple categories were created under the root level to divide the database into pieces called domains . Each domain contains DNS servers that are responsible for maintaining the database of computer names for that area of the database (that's the distributed part).

The first (or top) level of distribution is divided into domains based on country codes. Additional top-level domains for specific organizations were created to prevent the country domains from getting overcrowded.
Domain
|
Description
|
.mil
|
U.S. military sites |
.com
|
Commercial organizations |
.edu
|
Educational institutions |
.gov
|
U.S. government organizations |
.net
|
Internet Service Providers (ISPs) |
.us
|
Other U.S. organizations (such as local governments) |
.org
|
Nonprofit organizations |
.ca
|
Canadian organizations |
.de
|
German organizations |
[other countries]
|
Organizations from other countries |
Using DNS
The last way to determine system IP information is to utilize the C# DNS (Domain Name System) classes. DNS can be used to obtain Internet hostnames and IP Addresses. The C# System.Net namespace contains the DNS class, which comprises several methods that allow you to obtain DNS information about hosts. The GetHostName() method retrieves the hostname of the local system. The GetHostByName() method attempts to find the IP address of a hostname.
DNS Configuration
When your C# application is running on a customer's system, you have no guarantee that there are any DNS servers configured. You can find out what (if any) DNS servers are configured by using the .NET Registry class methods to examine the DNS Registry values.
Fortunately, all Windows platforms store the DNS server information in the same place:
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
Registry Data Values for DNS Servers
Data Value
|
Description
|
DatabasePath
|
The location of the host's file
|
Domain
|
The name of the system's domain
|
NameServer
|
The list of DNS servers
|
Hostname
|
The name of the system's DNS host
|
SearchList
|
A list of DNS domains to append to the end of hostnames in DNS name searches
|
The value of most interest here is NameServer . This should contain a single string value representing all the configured DNS servers, separated by spaces. The primary DNS server will be listed first, followed by the alternate servers.
Example: Demonstrate DNS and its Configuration in Registry
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Microsoft.Win32;
namespace _CSharpApplication
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
string _hostName = Dns .GetHostName();
lstDNS.Items.Add( "Local hostname :: " + _hostName);
IPHostEntry _ipHostEntry = Dns .GetHostByName(_hostName);
foreach ( IPAddress _address in _ipHostEntry.AddressList)
{
lstDNS.Items.Add( "IP Address ::" + _address.ToString());
}
}
private void button2_Click( object sender, EventArgs e)
{
RegistryKey start = Registry .LocalMachine;
string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" ;
RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
if (DNSserverKey == null )
{
lstDNSConfiguration.Items.Add( "Unable to open DNS servers key" );
return ;
}
string serverlist = ( string )DNSserverKey.GetValue( "NameServer" );
lstDNSConfiguration.Items.Add( "DNS Servers: " + serverlist);
DNSserverKey.Close();
start.Close();
char [] token = new char [1];
token[0] = ' ' ;
string [] servers = serverlist.Split(token);
foreach ( string server in servers)
{
lstDNSConfiguration.Items.Add( "DNS server: " + server);
}
}
}
}
Output:

Click on both the buttons will display the respective outputs in the list box specified below
|