Network cables

C#: Select All Active Local IP Addresses

To select all active local IP addresses (v4), use the following C# code:

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

// …

private static IEnumerable<IPAddress> GetLocalV4Addresses()
{
    return from iface in NetworkInterface.GetAllNetworkInterfaces()
           where iface.OperationalStatus == OperationalStatus.Up
           from address in iface.GetIPProperties().UnicastAddresses
           where address.Address.AddressFamily == AddressFamily.InterNetwork
           select address.Address;
}

Freelance full-stack .NET and JS developer and architect. Located near Cologne, Germany.

Leave a Reply

Your email address will not be published. Required fields are marked *