How to Get Local IP Address in C#

Muhammad Maisam Abbas Feb 16, 2024
  1. Get Local IP Address With the Dns.GetHostEntry() Function in C#
  2. Get Local IP Address With the Socket.LocalEndPoint Property in C#
  3. Get Local IP Address With Linq in C#
  4. Get Local IP Address With NetworkInterface Class in C#
How to Get Local IP Address in C#

In this tutorial, we will discuss methods to get the local IP Address of our machine in C#.

Get Local IP Address With the Dns.GetHostEntry() Function in C#

The Dns class gets the information related to a host on the Internet. There are many methods in the Dns class for DNS-related functionality in C#. The Dns.GetHostEntry() function is used to get the IP address of a host.

The following code example shows us how we can get our machine’s local IP address with the Dns.GetHostEntry() function in C#.

using System;
using System.Net;

namespace get_local_ip_address {
  class Program {
    static void Main(string[] args) {
      String strHostName = string.Empty;
      IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
      IPAddress[] addr = ipEntry.AddressList;

      for (int i = 0; i < addr.Length; i++) {
        Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
      }
    }
  }
}

Output:

IP Address 0: fe80::1db8:23a7:6483:8976%20
IP Address 1: 192.168.43.239

In the above code, we passed the Dns.GetHostName() function as a parameter of the Dns.GetHostEntry() function to get the IP address of our local machine in C#. The only problem with this method is that it gives us all the IP addresses of the machine. To get the specific IP address, we have to write the following code in C#.

using System;
using System.Net;

namespace get_local_ip_address {
  class Program {
    static void Main(string[] args) {
      var host = Dns.GetHostEntry(Dns.GetHostName());
      foreach (var ip in host.AddressList) {
        if (ip.AddressFamily == AddressFamily.InterNetwork) {
          Console.WriteLine("IP Address = " + ip.ToString());
        }
      }
    }
  }
}

Output:

IP Address = 192.168.43.239

In the above code, we checked whether the ip.AddressFamily was equal to AddressFamily.InterNetwork. This condition checks whether the IP Address in the ip variable is an IPv4 address or not. The above code only returns the IPv4 address of our machine. If we want to get the IPv6 address of our machine, we can use the AddressFamily.InterNetworkV6 attribute instead of the AddressFamily.InterNetwork.

Get Local IP Address With the Socket.LocalEndPoint Property in C#

A more accurate way of getting the local IP address of our machine when there are multiple IP addresses is to connect a UDP socket and then reading its local endpoint. The Socket class in C# implements the Berkeley sockets interface, which provides functionality for network communication. The following code example shows us how we can use the Socket.LocalEndPoint property to determine the local IP address of our machine in C#.

using System;
using System.Net;
using System.Net.Sockets;

namespace get_local_ip_address {
  class Program {
    static void Main(string[] args) {
      string localIP = string.Empty;
      using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) {
        socket.Connect("8.8.8.8", 65530);
        IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
        localIP = endPoint.Address.ToString();
      }
      Console.WriteLine("IP Address = " + localIP);
    }
  }
}

Output:

IP Address = 192.168.43.239

In the above code, we connected the socket Socket to our local machine and extracted the local endpoint of the socket with the socket.LocalEndPoint attribute. We used the endPoint.Address attribute to get the IP address of the endPoint.

Get Local IP Address With Linq in C#

The Linq or Language integrated query provides SQL functionality in C#. We can also use Linq and the Dns.GetHostEntry() function to get our machine’s local IP address in C#.

The following code example shows us how we can use Linq to get our machine’s local IP address in C#.

using System;
using System.Linq;
using System.Net;

namespace get_local_ip_address {
  class Program {
    static void Main(string[] args) {
      if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
        Console.WriteLine("No Network Available");
      }

      IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());

      var ippaddress =
          host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
      Console.WriteLine(ippaddress);
    }
  }
}

Output:

192.168.43.239

In the above code, we determined the local IPv4 address of our machine using Linq with the Dns.GetHostEntry() function in C#. The rest of the code is similar to our first example; the only difference is that we extracted only one IPv4 address from the list of addresses returned by the Dns.GetHostEntry() function.

Get Local IP Address With NetworkInterface Class in C#

The NetworkInterface class contains data about the network interfaces on our local machine and provides statistical and configuration for the network interfaces. The GetAllNetworkInterfaces() function in the NetworkInterface class gives us all the network interfaces on our local machine. The NetworkInterfaceType property in the NetworkInterface class is used to get the type of the network interface.

The following code example shows how we can use all these functions to get our local machine’s local IP address in C#.

using System;
using System.Linq;
using System.Net;

namespace get_local_ip_address {
  class Program {
    public static void getLocalIPAddressWithNetworkInterface(NetworkInterfaceType _type) {
      string output = "";
      foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) {
        if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) {
          foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) {
            if (ip.Address.AddressFamily == AddressFamily.InterNetwork) {
              output = ip.Address.ToString();
            }
          }
        }
      }
      Console.WriteLine("IP Address = " + output);
    }
    static void Main(string[] args) {
      getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Wireless80211);
    }
  }
}

Output:

IP Address = 192.168.43.239

We retrieved the WLAN IPv4 address of our local machine with the NetworkInterface class in C# in the above code. The above code can be used to get any type of IP address on our local machine. For example, if we want to get the Ethernet interface’s IP address, we have to specify it in the function call by passing NetworkInterfaceType.Ethernet.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Network