Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Find the IP Address of the Machine

  • Last Updated : 19 Oct, 2021

An IP address is known as an Internet Protocol address. It is a unique address that identifies the device over the network. It is almost like a set of rules which govern the data sent over the Internet or through a local network. It helps the Internet to distinguish between routers, computers, websites, etc. In this article, we are going to learn how to find the IP Address of the Machine using C#.

Using GetHostByName() method 

We can find the IP address of the machine using the GetHostByName() method. This method returns the DNS information for the given DNS hostname. When you pass an empty string in this method, then it will return the standard hostname of the local computer.

Syntax:

public static System.Net.IPHostEntry GetHostByName (string hName);

Here, hName is the DNS name of the host.



Approach:

To find the IP address of the machine follow the following steps:

  • Firstly include System.Net.
  • We need to find the name of host to get the IP Address of host. So, the name of host can be retrieved by using the GetHostName() method from the Dns class.
  • By passing the hostname to GetHostByName() method we will get the IP Address.
  • This method returns a structure of type hostent for the specified host name.
  • AddressList[0] gives the ip address and ToString() method is used to convert it to string.

Example:

C#




// C# program to print the IP address of the machine
using System;  
using System.Text;  
using System.Net;
  
class GFG{
      
static void Main(string[] args)  
{
      
    // Get the Name of HOST  
    string hostName = Dns.GetHostName(); 
    Console.WriteLine(hostName);  
      
    // Get the IP from GetHostByName method of dns class.
    string IP = Dns.GetHostByName(hostName).AddressList[0].ToString();  
    Console.WriteLine("IP Address is : " + IP);  
}  
}

Output:

IP Address is : 192.168.122.136

Using GetHostEntry() Method

We can also find the IP address of the machine using the GetHostEntry() method. This method queries DNS server and returns the IP address to the IPHostEntry instance. 

Syntax:

public static System.Net.IPHostEntry GetHostEntry (IPAddress address);

Approach:

To find the IP address of the machine follow the following steps:

  • Firstly include System.Net.
  • We need to find the name of the host to get the IP Address of the host. So, the name of the host can be retrieved by using the GetHostName method from the DNS class.
  • Bypassing the hostname to GetHostEntry( ) method we will get the IP Address.
  • This method returns a structure of type hostent for the specified hostname.
  • AddressList[0] gives the IP address and the ToString() method is used to convert it to string.

Example:

C#




// C# program to print the IP address of the machine
using System;  
using System.Text;  
using System.Net;
  
class GFG{
      
static void Main() 
{
    
    // Getting host name
    string host = Dns.GetHostName();
      
    // Getting ip address using host name
    IPHostEntry ip = Dns.GetHostEntry(host);
    Console.WriteLine(ip.AddressList[0].ToString());
}
}

Output:

IP Address is : 192.168.122.136



My Personal Notes arrow_drop_up
Recommended Articles
Page :