C# Code Snippet - Get local computer IP address

C# Code Snippet - Get local computer IP address

C# Code Snippet - Get local computer IP address

(C-Sharp) C# code snippet get the IP address from the local machine. For computers with multiple IP addresses with one or more NIC cards can use IP address object list to obtain all the IP addresses.

Bookmark:

C# Code Snippet - Get local computer IP address

This .Net C# code snippet get the IP address from the local machine. This function uses System.Net namespace to get local machine hostname and then get a array of IPAddress objects. Modify the exception handling section to meet your project requirements.

/// <summary>
/// Gets IP addresses of the local computer
/// </summary>
public string GetLocalIP()
{
    string _IP = null;

    // Resolves a host name or IP address to an IPHostEntry instance.
    // IPHostEntry - Provides a container class for Internet host address information. 
    System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

    // IPAddress class contains the address of a computer on an IP network. 
    foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
    {
        // InterNetwork indicates that an IP version 4 address is expected 
        // when a Socket connects to an endpoint
        if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
        {
            _IP = _IPAddress.ToString();
        }
    }
    return _IP;
}


Here is a simple example showing how to use above function (GetLocalIP) to display local machine IP address.

Console.WriteLine("Local computer IP address : " + GetLocalIP());


This .Net code snippet gets the array of all the IPAddress objects. Use this method if you are writing sockets applications, a computer can have multiple IP addresses with one or more NIC cards.

/// <summary>
/// Gets IP addresses object list from the local computer
/// </summary>
public System.Net.IPAddress[] GetLocalIPList()
{
    // Resolves a host name or IP address to an IPHostEntry instance.
    // IPHostEntry - Provides a container class for Internet host address information. 
    System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

    return _IPHostEntry.AddressList;
}


Here is a simple example showing how to use above function (GetLocalIPList) to display all the IP addresses from local machine.

// display all IP addresses of local machine
foreach (System.Net.IPAddress _IPAddress in GetLocalIPList())
    Console.WriteLine(_IPAddress.ToString());


// display all IP addresses of local machine with addressing scheme
foreach (System.Net.IPAddress _IPAddress in GetLocalIPList())
    Console.WriteLine(_IPAddress.AddressFamily.ToString() + " = " +  _IPAddress.ToString());


C# Keywords Used:

  • DNS
  • IPAddress
  • GetHostEntry
  • IPHostEntry
  • AddressList
  • AddressFamily
  • InterNetwork
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C#, CLI, DNS, Domain Name System, IPAddress, GetHostEntry, IPHostEntry, AddressFamily, InterNetwork, Socket programming, Networking, Network monitoring
  • Programming Language : C# (C-Sharp)

External Resources:

Sonny Eugenio :: August 12-2010 :: 04:20 AM

Is it possible to get the local IP address of the first network adapter directly without looping through the IP list.

JP van Mackelenbergh :: January 27-2011 :: 11:26 AM

Whats up with this:
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")

AddressFamily is an enumeration, so please give a good example :)
if (_IPAddress.AddressFamily == AddressFamily.InterNetwork)

Leave a comment