The code shows how to take a known MAC (e.g. "00:00:00:11:22:33") and locate the NIC which has that MAC. Note that the built-in MAC class for .NET is called PhysicalAddress (in System.Net.NetworkInformation). PhysicalAddress.Parse can take a string, so long as it has only numbers or numbers and dashes (-). We normally use colons (:) instead of dashes, so that's why I do a .Replace().
The last part of the code retrieves the IP Addresses associated with the selected NIC.
- using System.Net.Sockets;
- using System.Net;
- using System.Net.NetworkInformation;
- string localNicMac = "00:00:00:11:22:33".Replace(":", "-"); // Parse doesn't like colons
- var mac = PhysicalAddress.Parse(localNicMac);
- var localNic =
- .Where(nic => nic.GetPhysicalAddress().Equals(mac)) // Must use .Equals, not ==
- if (localNic == null)
- throw new ArgumentException("Local NIC with the specified MAC could not be found.");
- var ips =
- .Select(x => x.Address);
You need to login to post a comment.