How to

get mac address

Published: 9. October 2011 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Windows » Hardware, Network
Tags: C# Hardware Network Windows
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;

Code

public static string[] GetPhysicalAddresses()
{
    var result = new List<string>();
    NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in interfaces)
    {
        PhysicalAddress address = adapter.GetPhysicalAddress();
        byte[] bytes = address.GetAddressBytes();
        if (bytes.Length == 0) continue;

        var mac = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            mac.Append(bytes[i].ToString("X2"));
            if (i != bytes.Length - 1) mac.Append("-");
        }

        result.Add(mac.ToString());
    }
    return result.ToArray();
}
Send us feedback about this snippet »



Related Snippets: