gln gln - 1 year ago 180
C# Question

Formatting MAC address in C#

In my C# application, I want to get my MAC address by using

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:

using the code above ONLY?

Answer Source


mac = string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download