Extracting MAC address using C#

A MAC address (Media Access Control address) is a unique identifier assigned to network interfaces for communications at the data link layer of a network segment. It serves as a network address for most IEEE 802 network technologies, including Ethernet, Wi-Fi, and Bluetooth.

In C#, you can extract MAC addresses using the NetworkInterface class from the System.Net.NetworkInformation namespace. This class provides methods to enumerate all network interfaces on the local computer and retrieve their physical addresses.

Syntax

Following is the syntax for getting all network interfaces −

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

Following is the syntax for getting the MAC address of a network interface −

string macAddress = networkInterface.GetPhysicalAddress().ToString();

Getting MAC Address of Active Interface

This example retrieves the MAC address of the first active network interface −

using System;
using System.Net.NetworkInformation;

class Program {
   public static void Main() {
      string macAddress = GetMACAddress();
      if (!string.IsNullOrEmpty(macAddress)) {
         Console.WriteLine("MAC Address: " + macAddress);
      } else {
         Console.WriteLine("No active network interface found.");
      }
   }

   public static string GetMACAddress() {
      foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) {
         if (networkInterface.OperationalStatus == OperationalStatus.Up) {
            return networkInterface.GetPhysicalAddress().ToString();
         }
      }
      return "";
   }
}

The output of the above code is −

MAC Address: 1A2B3C4D5E6F

Getting All MAC Addresses

This example retrieves MAC addresses of all network interfaces along with their names and types −

using System;
using System.Net.NetworkInformation;

class Program {
   public static void Main() {
      Console.WriteLine("All Network Interfaces:");
      Console.WriteLine("------------------------");
      
      foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) {
         string macAddress = networkInterface.GetPhysicalAddress().ToString();
         if (!string.IsNullOrEmpty(macAddress)) {
            Console.WriteLine("Name: " + networkInterface.Name);
            Console.WriteLine("Type: " + networkInterface.NetworkInterfaceType);
            Console.WriteLine("Status: " + networkInterface.OperationalStatus);
            Console.WriteLine("MAC Address: " + FormatMACAddress(macAddress));
            Console.WriteLine();
         }
      }
   }

   public static string FormatMACAddress(string macAddress) {
      string formatted = "";
      for (int i = 0; i < macAddress.Length; i += 2) {
         if (i > 0) formatted += ":";
         formatted += macAddress.Substring(i, 2);
      }
      return formatted;
   }
}

The output of the above code is −

All Network Interfaces:
------------------------
Name: Ethernet
Type: Ethernet
Status: Up
MAC Address: 1A:2B:3C:4D:5E:6F

Name: Wi-Fi
Type: Wireless80211
Status: Up
MAC Address: AA:BB:CC:DD:EE:FF

Getting MAC Address by Interface Type

This example retrieves the MAC address of a specific network interface type, such as Ethernet −

using System;
using System.Net.NetworkInformation;

class Program {
   public static void Main() {
      string ethernetMAC = GetMACAddressByType(NetworkInterfaceType.Ethernet);
      string wifiMAC = GetMACAddressByType(NetworkInterfaceType.Wireless80211);
      
      Console.WriteLine("Ethernet MAC: " + (ethernetMAC ?? "Not found"));
      Console.WriteLine("Wi-Fi MAC: " + (wifiMAC ?? "Not found"));
   }

   public static string GetMACAddressByType(NetworkInterfaceType interfaceType) {
      foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) {
         if (networkInterface.NetworkInterfaceType == interfaceType && 
             networkInterface.OperationalStatus == OperationalStatus.Up) {
            return networkInterface.GetPhysicalAddress().ToString();
         }
      }
      return null;
   }
}

The output of the above code is −

Ethernet MAC: 1A2B3C4D5E6F
Wi-Fi MAC: AABBCCDDEEFF

Common Network Interface Types

NetworkInterfaceType Description
Ethernet Wired Ethernet connection
Wireless80211 Wi-Fi wireless connection
Loopback Loopback interface (localhost)
Tunnel Virtual tunnel interface

Key Points

  • The GetPhysicalAddress() method returns a PhysicalAddress object that represents the MAC address.

  • Not all network interfaces have MAC addresses − virtual interfaces may return empty addresses.

  • Check the OperationalStatus property to ensure the interface is active before retrieving its MAC address.

  • MAC addresses are typically displayed in hexadecimal format with colons or hyphens as separators.

Conclusion

Extracting MAC addresses in C# is straightforward using the NetworkInterface class. You can retrieve MAC addresses from active interfaces, filter by interface type, or enumerate all available network interfaces to get their physical addresses for network identification and management purposes.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements