Search code examples
c#stringformatmac-address

Formatting MAC address in C#


In my C# application, I want to get my MAC address by using NetworkInterface class as the following:

NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()
{
    mac = nic.GetPhysicalAddress()
}

But this code returns the MAC without ':' or any other separator.

How can I retrieve the MAC in this format: 88:88:88:88:87:88 using the code above ONLY?


Solution

  • try

    mac = string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());