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?
command. RUN. 2 In the RUN command type CMD and click OK. When the black Command window appears, type IPCONFIG.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With