Getting IP Address and Host Name Using Dns Class

In general, when we need to find an IP Address of a machine, we run the ipconfig command in a command prompt.
 
 
We can also find it using C# using the DNS Class. The Dns(Domain Name Service) class is present in the System.Net namespace that has methods to retrieve IP Addresses, Host Names and so on.
 
Example: Program for getting Host Name 
First, add the System.Net namespace in a namespace declaration. The GetHostName() method of the Dns class returns the host name of the local computer. 
  1. string HostName = Dns.GetHostName();  
  2. Console.WriteLine("Name of machine ="+HostName);  
Preview:
 
 
Example: Program For getting the IP Address of Host (Local).
The Dns class provides the GetHostAddresses() method that takes HostName as a parameter and returns an array of IPAddress. We get both IPv4 and IPv6 of the machine.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6.   
  7. namespace GetHostNameAndIPAddress  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string HostName = Dns.GetHostName();  
  14.             Console.WriteLine("Host Name of machine ="+HostName);  
  15.             IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);  
  16.             Console.WriteLine("IP Address of Machine is");  
  17.             foreach(IPAddress ip in ipaddress)  
  18.             {  
  19.                 Console.WriteLine(ip.ToString());  
  20.             }  
  21.         }  
  22.     }  
  23. }  
 Preview:
 
 
Example: Program for getting the IPAddress of a Domain Name: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6.   
  7. namespace GetIPusingHostname  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string HostName = "www.bing.com";  
  14.             IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);  
  15.             Console.WriteLine("IPAddress of " + HostName + " is");  
  16.             foreach (IPAddress ipaddr in ipaddress)  
  17.             {  
  18.                 Console.WriteLine(ipaddr);  
  19.             }  
  20.         }  
  21.     }  
  22. }  
Preview: 
 
 
Example: Program for getting IPv4 and IPv6 separately by checking the Address Family: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6.   
  7. namespace GetIPv4andIPv6Seperate  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string HostName = Dns.GetHostName();  
  14.             Console.WriteLine("Host Name of machine =" + HostName);  
  15.             IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);  
  16.             Console.Write("IPv4 of Machine is ");  
  17.             foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork))  
  18.             {  
  19.                 Console.WriteLine(ip4.ToString());  
  20.             }  
  21.             Console.Write("IPv6 of Machine is ");  
  22.             foreach (IPAddress ip6 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))  
  23.             {  
  24.                 Console.WriteLine(ip6.ToString());  
  25.             }  
  26.         }  
  27.     }  
  28. }  
Preview:
 
 
Example: Program for getting Host Name based on IPAddress.
The getHostEntry() method of the DNS class takes IPAddress as parameter and returns IPHostEntry that contains address information about the host specified in the address.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6.   
  7. namespace GetHostNameUsingIP  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string IPAdd = "204.79.197.200";  
  14.             IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd);  
  15.             Console.WriteLine(hostEntry.HostName);  
  16.         }  
  17.     }  
  18. }  
Preview:
 

I hope you like it. Thanks.


Similar Articles