1

when using System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName())to get IP addresses and

    private System.Net.NetworkInformation.PhysicalAddress GetMacAddress()
    {
        foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
        {
            // Only consider Ethernet network interfaces
            if (nic.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet &&
                nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
            {
                return nic.GetPhysicalAddress();
            }
        }
        return null;
    }

to get the MAC adddress, I get all addresses. Is there a way to get only the adapter and IP that actually connects to the net?

With above calls, I also get IP's and MAC's belonging to virtual adapters, such as the created by VMWare.

Regards Jaime

17
  • You have to go through the ARP table and get the mac associated with the IP. You can see table from cmd.exe >ARP -A
    – jdweng
    Jul 7, 2020 at 17:37
  • @jdweng I need to get the connected address using C#. Any idea?
    – jstuardo
    Jul 7, 2020 at 18:51
  • How do you tell which one connects to the net? It could be any of them except a loopback or localhost. Just being a physical NIC doesn't give any assurance that the address can get out.
    – Duston
    Jul 7, 2020 at 19:38
  • NIC is just looking at the ARP table. A ping is a type of ARP message. The PC automatically stores any ARP message that is one the subnet. Every computer when turned on sends out an ARP so all other machine in the subnet know that a new IP enters the subnet. Then sends out another ARP every 15 minutes. If a machine doesn't see an ARP for 30 minutes it remove the IP from ARP table. When a PING is requested the response from a PING is also save in table. So the MAC address for the local machine you can look up the IP in the Dns.GetHostName() and then use to find MAC.
    – jdweng
    Jul 7, 2020 at 19:52
  • Why do you say not the localhost? PC are configured differently and some machine have the loopback as localhost and some machines use the IP of the machine.
    – jdweng
    Jul 7, 2020 at 19:53

2 Answers 2

1

As a good approach, I am looking for the NIC that has a gateway set. This works in my case:

    private static IEnumerable<System.Net.NetworkInformation.NetworkInterface> GetAllNetworkInterfaces(IEnumerable<System.Net.NetworkInformation.NetworkInterfaceType> excludeTypes)
    {
        var all = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Where(nic => nic.GetIPProperties().GatewayAddresses.Any() && nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up);
        var exclude = all.Where(i => excludeTypes.Contains(i.NetworkInterfaceType));
        return all.Except(exclude);
    }

And then, use it like this:

                var nic = GetAllNetworkInterfaces(new[] { System.Net.NetworkInformation.NetworkInterfaceType.Tunnel, System.Net.NetworkInformation.NetworkInterfaceType.Loopback });
                txtRegistro.AppendText($"Local IP -> {string.Join(", ", nic.SelectMany(n => n.GetIPProperties().UnicastAddresses).Where(i => i.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Select(i => i.Address.ToString()))}\r\n");
                txtRegistro.AppendText($"MAC Address -> {string.Join(", ", nic.Select(a => string.Join(":", a.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2")))))}\r\n\r\n");

With above code, I am getting which NIC has a connection to a network (LAN or WAN) as long as the connected router provides a gateway.

Regards

Jaime

0

My adapter network interface have 5:

{"Id":"{FD94887A-FEEB-4F0B-AE11-D3A36137976B}","Name":"Ethernet","Description":"Intel(R) Ethernet Connection I217-LM","NetworkInterfaceType":6,"OperationalStatus":2,"Speed":-1,"IsReceiveOnly":false,"SupportsMulticast":true}
{"Id":"{765A365A-6D0B-4ACB-BCC8-779F481DC5D5}","Name":"Local Area Connection* 13","Description":"Microsoft Wi-Fi Direct Virtual Adapter #5","NetworkInterfaceType":71,"OperationalStatus":2,"Speed":-1,"IsReceiveOnly":false,"SupportsMulticast":true}
{"Id":"{D888C03B-82F1-4BBB-AB99-185F21B62159}","Name":"Local Area Connection* 14","Description":"Microsoft Wi-Fi Direct Virtual Adapter #6","NetworkInterfaceType":71,"OperationalStatus":2,"Speed":-1,"IsReceiveOnly":false,"SupportsMulticast":true}
{"Id":"{56ECAAAF-8916-4358-A8DB-FB6E7448133D}","Name":"Wi-Fi 4","Description":"Intel(R) Centrino(R) Ultimate-N 6300 AGN","NetworkInterfaceType":71,"OperationalStatus":1,"Speed":135000000,"IsReceiveOnly":false,"SupportsMulticast":true}
{"Id":"{80377EDA-E86D-11E8-BA87-806E6F6E6963}","Name":"Loopback Pseudo-Interface 1","Description":"Software Loopback Interface 1","NetworkInterfaceType":24,"OperationalStatus":1,"Speed":1073741824,"IsReceiveOnly":false,"SupportsMulticast":true}

There are 3 attributes we need to care about: NetworkInterfaceType, OperationalStatus, Speed

This is my code:

private PhysicalAddress GetMacAddress()
{
    var allowNetworkInterfaceType = new[] { NetworkInterfaceType.Ethernet , NetworkInterfaceType.Wireless80211 , NetworkInterfaceType.GigabitEthernet };
    var networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x=>x.OperationalStatus == OperationalStatus.Up && allowNetworkInterfaceType.Contains(x.NetworkInterfaceType) && x.Speed > 0);
    return networkInterface?.GetPhysicalAddress();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.