86

I need to know how to get all network interfaces with their IPv4 address. Or just wireless and Ethernet.

To get all network interfaces details I use this:

foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {
    if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
       ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {

        Console.WriteLine(ni.Name);
    }
}

And to get the all hosted IPv4 addresses of the computer:

IPAddress [] IPS = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in IPS) {
    if (ip.AddressFamily == AddressFamily.InterNetwork) {

        Console.WriteLine("IP address: " + ip);
    }
}

But how to get the network interface and its right ipv4 address?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185
  • 1
    Please read a little more carefully. See [GetIPProperties](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getipproperties.aspx) – John Saunders Mar 24 '12 at 20:22
  • @JohnSaunders okay i have checked your link read it .. and tried it .. but i didn't get the IPV4 Address !! like 192.168.1.25 !! – Murhaf Sousli Mar 24 '12 at 20:35
  • 3
    Ok, it's a little more subtle than I thought. See [IPGlobalProperties.GetUnicastAddresses](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getunicastaddresses.aspx) – John Saunders Mar 24 '12 at 20:47

4 Answers4

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

This should get you what you want. ip.Address is an IPAddress, that you want.

bwall
  • 1,501
  • 1
  • 10
  • 6
9

One line with Lamda:

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

var ipV4s = NetworkInterface.GetAllNetworkInterfaces()
    .Select(i => i.GetIPProperties().UnicastAddresses)
    .SelectMany(u => u)
    .Where(u => u.Address.AddressFamily == AddressFamily.InterNetwork)
    .Select(i => i.Address);
Mc_Topaz
  • 567
  • 1
  • 6
  • 21
3

With some improvement, this code adds any interface to a combination:

private void LanSetting_Load(object sender, EventArgs e)
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) || (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) //&& (nic.OperationalStatus == OperationalStatus.Up))
        {
            comboBoxLanInternet.Items.Add(nic.Description);
        }
    }
}

And when selecting one of them, this code returns the IP address of the interface:

private void comboBoxLanInternet_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
        {
            if (nic.Description == comboBoxLanInternet.SelectedItem.ToString())
            {
                if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    MessageBox.Show(ip.Address.ToString());
                }
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hady Mahmoodi
  • 99
  • 1
  • 4
3

These answers lead me in the right direction - thanks to previous authors! For anyone looking for plug and play methods, here is what worked for me.

public System.Net.IPAddress GetIpAddress()
{
    return NetworkInterface
        .GetAllNetworkInterfaces()
        .Where(n => n.OperationalStatus == OperationalStatus.Up)
        .Where(n => n.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || n.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
        .Where(n => n.Name == "Wi-Fi")
        .SelectMany(n => n.GetIPProperties()?.UnicastAddresses)
        .Where(n => n.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
        .Select(g => g?.Address)
        .Where(a => a != null)
        .FirstOrDefault();
}

public System.Net.IPAddress GetDefaultGateway()
{
    return NetworkInterface
        .GetAllNetworkInterfaces()
        .Where(n => n.OperationalStatus == OperationalStatus.Up)
        .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
        .SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
        .Select(g => g?.Address)
        .Where(a => a != null)
        .FirstOrDefault();
}