How to get the system MAC address using C#?

Media Access Control (MAC) address is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification and used in the Media Access Control protocol sub-layer. If assigned by the manufacturer, a MAC address usually encodes the manufacturer’s registered identification number. It also be known as an Ethernet Hardware Address (EHA).

In this article, I will demonstrate how to get system MAC address using C#.

public string GetMACAddress()
{
    var macAddress = string.Empty;
    NetworkInterface[] onjNI = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in onjNI)
    {
        if (string.IsNullOrEmpty(macAddress))
        {
           IPInterfaceProperties objIP = adapter.GetIPProperties();
           macAddress = adapter.GetPhysicalAddress().ToString();
 
           if(!string.IsNullOrEmpty(macAddress))
           {
               break;
           }
        }
    }
  return macAddress;
 }

Leave a Reply