There are some ways to get the MAC address on the network as follows:
1. Send the ARP command, using the returned MAC address buffer to get
2. Use Networkinterface.getallnetworkinterfaces () to obtain all network cards (including virtual network cards) to obtain the appropriate MAC address
3. Also available from the local registry:
ManagementObjectSearcher ManagementObjectSearcher = new ManagementObjectSearcher ("SELECT * from Win32_ Networkadapterconfiguration ");
Managementobjectcollection managementobjectcollection = Managementobjectsearcher.get ();
BOOL flag = FALSE;
using (managementobjectcollection.managementobjectenumerator enumerator = Managementobjectcollection.getenumerator ())
{
while (enumerator. MoveNext ())
{
The three methods of the 1th, 2 is better, when the machine has more than one network card, or virtual network card, the third in the WIN8 is not accurate. 1th, 2 can be based on the given parameter IP value to accurately locate the corresponding Mac Address.
The second type of code is as follows:
String macAddress = "";
networkinterface[] nics = networkinterface.getallnetworkinterfaces ();
foreach (NetworkInterface adapter in NICs)
{
if (!adapter. Getphysicaladdress (). ToString (). Equals (""))
{
MacAddress = adapter. Getphysicaladdress (). ToString ();
for (int i = 1; i < 6; i++)
{
MacAddress = Macaddress.insert (3 * i-1, ":");
}
Break
}
}
The first method is as follows:
public static string Getmac (String p_id)
{
IPAddress _address;
if (! Ipaddress.tryparse (p_id, out _address)) return "";
UINT Destip = System.BitConverter.ToUInt32 (_address.getaddressbytes (), 0);
ULONG pmacaddr = 0;
UINT Phyaddrlen = 6;
UINT Error_code = Sendarp (destip, 0, ref pmacaddr, ref Phyaddrlen);
byte[] _bytes1 = bitconverter.getbytes (PMACADDR);
Return bitconverter.tostring (_bytes1, 0, 6);
}
[DllImport ("Iphlpapi.dll")]
public static extern uint Sendarp (UINT destip, uint SRCIP, ref ulong PMACADDR, ref uint Phyaddrlen);
about how to get the native MAC address