How to get DNS Servers from Registry

We can get the DNS Server names from the Registry.
The following code is used to get the DNS Server names from registry

Name Space


using System;
using Microsoft.Win32;




class MyDNSServers {
public static void Main()
{
RegistryKey MyRegistryKey = Registry.LocalMachine;
string MyDNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
RegistryKey MyDNSserverKey = MyRegistryKey.OpenSubKey(MyDNSservers);

if (MyDNSserverKey == null) {
Console.WriteLine("No DNS servers keys are there");
return;
}

string MyServerlist = (string) MyDNSserverKey.GetValue("NameServer");

MyDNSserverKey.Close();
MyRegistryKey.Close();

char[] MyKeys = new char[1];
MyKeys[0] = ' ';
string[] MyServerNames = MyServerlist.Split(MyKeys);
foreach (string Name in MyServerNames) {
Console.WriteLine("DNS server Name - {0}", Name);
}
}
}


Code Explanation

1. Create an instance of Registry key
2. Create an instance of Registry key by geting the DNS servers registry values
3. Get the list of DNS Server name using that registry key
4. By using foreach get each DNS Server name


By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: