Home About

December 8th, 2008

Finding all local IP Addresses in C# - 1

A common problem is the find out the IP address of the machine your C# program is running on. C# provides two methods for obtaining the information. The first one is easy; but somewhat undocumented. The other one is just a little harder. If your code has to run on Mono then the second option is your only alternative.

C# originally provided basic system information through the System.Net.DNS class. By querying the DNS “GetHostEntry” we could also discover the local IP addresses. The problem is that under Mono this only returns the local loop-back connector making this not a portable solution.

In .NET 2.0 the System.Net.NetworkInformation class was added, which allows you to browse all the network adapters and query their configuration in much greater detail.

The code below shows both methods for querying the local configuration:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Net.Sockets;

namespace IPLookup
{
    class MainClass
    {
        public static IPAddress[] GetAllUnicastAddresses_Old()
        {
            // By passing an empty string to GetHostEntry we receive all the IP addresses on the local machine
            // This breaks on Mono
            IPHostEntry LocalEntry = Dns.GetHostEntry("");
            return LocalEntry.AddressList;
        }

        public static IPAddress[] GetAllUnicastAddresses_New()
        {
            // This works on both Mono and .NET , but there is a difference: it also
            // includes the LocalLoopBack so we need to filter that one out
            List<IPAddress> Addresses = new List<IPAddress>();
            // Obtain a reference to all network interfaces in the machine
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
                {
                    // Ignore loop-back addresses & IPv6
                    if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily!= AddressFamily.InterNetworkV6)
                      Addresses.Add(uniCast.Address);
                }

            }
            return Addresses.ToArray();
        }

        public static void Main(string[] args)
        {
            IPAddress[] Adresses = GetAllUnicastAddresses_Old();
            foreach (IPAddress Adres in Adresses)
            {
                Console.WriteLine("OLD IP Address: {0}", Adres);
            }

            IPAddress[] Adresses2 = GetAllUnicastAddresses_New();
            foreach (IPAddress Adres in Adresses2)
            {
                Console.WriteLine("NEW IP Address: {0}", Adres);
            }
        }

    }
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Reddit

Tags: ,

One Response to “Finding all local IP Addresses in C#”

  1. Querying System.Net.NetworkInformation in C# | Dijksterhuis.Org Says:

    [...] Dijksterhuis.Org « Finding all local IP Addresses in C# [...]

Leave a Reply


Recent Comments
  • Ales: Hi, Thanks for the code… I must say I did not experience any errors decrypting any of my messages. I even...
  • JC: Thanks very useful and well explained
  • Thomas: This is a public static class written in the C# language that does not save state. You can call into the...
  • Simon: Thank you very much for this post! It helped me essentially to overcome obstacels to work with mono!
  • Graham: This is a good research for keyboard shortcuts! Some shortcuts are also compatible for Windows OS. I have...