Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the network interface and its right IPv4 address?

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?

like image 503
Murhaf Sousli Avatar asked Mar 24 '12 20:03

Murhaf Sousli


People also ask

How do I find my network interface IP address?

command. RUN. 2 In the RUN command type CMD and click OK. When the black Command window appears, type IPCONFIG.

How do I find my network interface card Windows 10?

Windows 8 and 10 usersIn the System Information window, click the + symbol next to Components in the left navigation area. Click the + next to Network and highlight Adapter. The right side of the window should display complete information about the network card.

What is network interface address?

Each network interface must have its own unique IP address. The IP address that you give to a host is assigned to its network interface, sometimes referred to as the primary network interface. If you add a second network interface to a machine, it must have its own unique IP number.


1 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.

like image 91
bwall Avatar answered Oct 14 '22 06:10

bwall