Atomico
Atomico

Reputation: 473

C# get all local ip address

I am using this code to find all ip address from all interface:

foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{    
    if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
    {
        foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
        {
            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                ...
            }
        }
    }
}

The problem is on some pc it show the ip 169.254.x.x (i know this is the "default" ip address.. useless here.)

if i do ipconfig /all i don't see this ip, but the real question is how can i avoid to show this ip?

Upvotes: 0

Views: 3021

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

In this case it would seem like a good fit to use IsDnsEligible to eliminate the auto ip addresses;

foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
    if(!ip.IsDnsEligible)
        continue;

    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
    ...

Upvotes: 3

Related Questions