about how to get the native MAC address

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.