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;
           }
       }

How to calculate md5 of a string in C#

Filed Under (c#, cryptography) by The Chef on 26-05-2009

Tagged Under : , , ,

If you have ever dared to write the md5 hashing algorithm in C#, you better stay calm because the MS guys thought of everything. It’s all in the framework. With just 2 lines of code the job is done. Take a look at the following piece of code:

String str="some text here";
MD5 md5 = new MD5CryptoServiceProvider();
String Hash = BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(str))).Replace("-","");

Easy, isn’t it ?

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 : , , ,