This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 2.0+

Obtaining a MAC Address

A MAC address is a twelve digit hexadecimal number that uniquely identifies a network interface, such as a network card in a computer or printer. Using the .NET framework, the MAC addresses of the network interfaces in a computer can be found.

MAC Addresses

All devices that are connected together using a network must have a unique identifier in order that messages can be correctly passed between them. Many network technologies use a Media Access Control address, known as a MAC address, for this purpose.

A MAC address is a six byte code that is assigned to each network interface in a device. Many devices have only one MAC address, as they have only one connection. For example, a printer connected to a small local area network (LAN) will generally require only one network interface and, therefore, a single MAC address. Other devices have multiple MAC addresses. These include computers that have more than one network card. For example, many notebook computers have two MAC addresses; one for a wired network port and one for a wireless adapter.

The six bytes of a MAC address provide over 280 trillion possible values that can be formatted in various ways. It is common to see six pairs of hexadecimal digits, delimited by hyphens or colons, such as 01-23-45-67-89-AB or 01:23:45:67:89:AB. Sometimes the digits are grouped into three sets of four, such as 0123.4567.89AB. Finally, in this article, we will see MAC addresses with no punctuation, such as 0123456789AB.

There are some situations where it can be useful to obtain the MAC addresses that are in use on the local computer. One common use is to limit the execution of software according to a licensing scheme. The MAC addresses, combined with other identifying information, can be used to try to prevent your software from being illegally copied from one computer to another. In this article we will see how to obtain the MAC addresses for each operational network card in the local machine using standard .NET framework classes.

NetworkInterface Class

To obtain information about network interfaces in a computer, including the MAC addresses, we can use the NetworkInterface class. We start by obtaining an array of NetworkInterfaces, one for each physical and logical network card, by calling the GetAllNetworkInterfaces static method. This is shown below:

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

Once we have the array, we can loop through it to find the network interfaces that we are interested in and their MAC addresses and other details. There are three other properties that we are interested in. Two simple string properties are Name and Description, which provide useful textual information to identify each network interface. We will use a third property to filter the network interfaces. The OperationalStatus property describes the current state of the network card. It returns a value from the OperationalStatus enumerated type with the following possible values:

  • Dormant. The network interface is waiting for an external event before it becomes available.
  • Down. The network card is currently unavailable.
  • LowerLayerDown. The network interface is reliant upon one or more other interfaces and at least one of those dependencies is unavailable.
  • NotPresent. The network interface is not present. This can be because a hardware component is missing.
  • Testing. The network interface is unavailable as it is performing tests.
  • Unknown. The status of the network interface cannot be determined.
  • Up. The network interface is available for use.

Finally, to obtain the MAC address of a network interface we will use the GetPhysicalAddress method. This returns a PhysicalAddress object that represents the address of a network card. The MAC address can be extracted from this object in string form using the ToString method or as an array of bytes using the GetAddressBytes method.

The code below shows the classes, methods and properties in action. The foreach loop processes each NetworkInterface object in turn. However, only network cards that are currently available and that have a MAC address are shown. The comments show the sample output for a single network interface.

foreach (NetworkInterface ni in interfaces)
    if (ni.OperationalStatus ==
        OperationalStatus.Up && ni.GetPhysicalAddress().GetAddressBytes().Length != 0)
        Console.WriteLine("Name        : {0}", ni.Name);
        Console.WriteLine("Description : {0}", ni.Description);
        Console.WriteLine("MAC Address : {0}", ni.GetPhysicalAddress().ToString());


Name        : Wireless Network Connection
Description : Realtek RTL8187B Wireless 802.11b/g 54Mbps USB 2.0 Network Adapter
MAC Address : 0123456789AB

4 December 2011