C# – Resolve Domain Name by System.Net

C# – Resolve Domain Name by System.Net

   DNS.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Net;
 
   namespace NetworkMonitor
   {
      public class DNS
      {
         private string HostName;
         private Notification notify;
         private Library lib;
 
         public DNS(string HostName)
         {
            this.HostName = HostName;
            this.notify = new Notification();
 
            this.lib = new Library();
         }
 
         public string getIPbyHostName()
         {
            try
            {
               System.Net.IPAddress[] result = System.Net.Dns.GetHostAddresses(this.HostName);
 
               if (result.Count() > 0)
               {
                  if (result[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                     return result[0].ToString();
                  else
                     return "Local Network.";
               }
 
               return "Cannot Resolve the Domain Name.";
            }
            catch (Exception ex)
            {
               return "Cannot Resolve the Domain Name.";
            }
         }
      }
   }