How to get the ip address in C#?

IP (Internet Protocol) Address is an address of your network hardware. It helps in connecting your computer to other devices on your network and all over the world. An IP Address is made up of numbers or characters.

All devices that are connected to an internet connection have a unique IP address which means there’s a need of billions of IP addresses. This requirement is fulfilled by the new IP version IPv6.

Private IP Address

A private IP address is the address of your device connected on the home or business network. If you have a few different devices connected to one ISP (Internet Service Provider), then all your devices will have a unique private IP address. This IP address cannot be accessed from devices outside your home or business network.

For example:


class Program{
   static void Main(string[] args){
      string IPAddress = GetIPAddress();
   public static string GetIPAddress(){
      string IPAddress = string.Empty;
      IPHostEntry Host = default(IPHostEntry);
      string Hostname = null;
      Hostname = System.Environment.MachineName;
      Host = Dns.GetHostEntry(Hostname);
      foreach (IPAddress IP in Host.AddressList){
         if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
            IPAddress = Convert.ToString(IP);
      return IPAddress;


Updated on: 05-Nov-2020


Kickstart Your Career

Get certified by completing the course

Get Started