Finding DNS servers using c#

Filed Under (c#, net) by The Chef on 06-06-2009

Tagged Under : , ,

Every computer connected to the internet must interrogate a DNS server in order to resolve domain names to ip addresses. To find the DNS server addresses your computer uses to resolve names, either it has a dhcp or static ip address, you can use the following piece of code. I wrapped all around a nice static class for ease of use.

   public static class DNSFinder
   {
       private static List<String> mServers = new List<string>();
       public static List<String> Servers
       {
           get
           {
               RegistryKey start = Registry.LocalMachine;
               string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
               RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
               if (DNSserverKey != null)
               {
                   // Static ip address
                   string serverlist = (string)DNSserverKey.GetValue("NameServer");
                   string[] servers = serverlist.Split(' ');
                   foreach (string server in servers)
                   {
                       mServers.Add(server);
                   }
                   //dhcp assigned address
                   serverlist = (string)DNSserverKey.GetValue("DhcpNameServer");
                   servers = serverlist.Split(' ');
                   foreach (string server in servers)
                   {
                       mServers.Add(server);
                   }
                   start.Close();
               }
               DNSserverKey.Close();
               return mServers;
           }
       }

10 tools a C/C++/C#/PHP programmer should never miss

Filed Under (c#, c/c++, mysql, net, php, system, tools) by The Chef on 13-05-2009

Tagged Under : , , ,

C#: Disable the Expect 100 Continue issue

Filed Under (c#, net) by The Chef on 26-04-2009

Tagged Under : ,

Here is a quick way to disable the Expect 100 Continue problem in C#. Simply put this line at the begining of your code:


System.Net.ServicePointManager.Expect100Continue = false;