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

I need to know how to get every network interface with its ipv4 address. or just wireless and ethernet.

i know that if i want 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?

Get network interface name from IPv4 address

Given an IPv4 address, how can I obtain its associated network interface name, like Ethernet adapter Local Area Connection, in Windows using C++? Alternately, how can I obtain a list of both network

How do I get the IPv4 subnetmask on interface with both v4 and v6 address?

I have an InterfaceAddress that returns an ipv4 address (4 octets). However the network prefix length seems to be for the ipv6 address associated with the interface (it’s returning as 128). How do I f

Docker container without ipv4 address

Under ubuntu 13.04 with docker 0.7.2 when I create a container via Dockerfile or interactively : the network interface generated veth** does not have an ipv4 address but an ipv6 one. How can I get a

How to calculate network mask length for ipv4 address using ipv6 network mask length?

I am trying to calculate ipv4 address in a range using network mask length. Ex: Starting IP(v6): ::ffff:1.0.64.0 Network mask length:: 114 How can calculate the ending ip(in v4 format) of this range u

How do I get IP_ADDRESS in IPV4 format

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done goo

List Network Adapter name with IPv4 address

Im trying to list the only ACTIVE network adapter with its IPv4 addresses on one computer. i have this code but it will list every network card either its connected or not. Private Sub Form1_Load(ByV

How to get IPv4 and IPv6 address of local machine?

I am developing a windows application and I need to find the IPv4 and IPv6 address of local machine. OS can be XP or Windows 7. I got a solution for getting MAC address like, string GetMACAddress() {

what is the best way to get two IPv6 networks to communicate over IPv4 network? [closed]

I have a setup of a laptop connected to a cellular IPv4 network (3G usb dongle) and PC connected to another IPv4/IPv6 network. Both have been configured to run as IPv6 routers and each has its IPv6 ne

How can I convert IPV6 address to IPV4 address?

I have application that uses IPV4 addresses (it stores them as long), so it only understands IPV4 addresses. Is it possible to convert IPV6 address to IPV4 with Java?

Retrieving the interface–to–IP address mapping table

I know the index of network interface returned by WinAPI’s GetBestInterface. How do I get interface properties (IPv4 address) based on interface’s index? Here is the working C++ code, but I need it in

Answers

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.

with some improvement this code add any interface to a combo

 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 select one of them this code message ip 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());
                    }
                }
            }
        }
    }