How to get the MAC address of system using

What is MAC (Media Access Control) address?

Media Access Control address (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 may also be known as an Ethernet Hardware Address (EHA), hardware address, adapter address, or physical address.

Why use?

MAC address is very commonly use in various application. It is generally used in voting application, poll application like this where identify a unique user is very important. There are many ways you can identify a unique user such as using the hard disk id, processor id etc for desktop application and for web application you can use ip address, cookies or session state server etc.

How to get MAC address Using C#:

Now I want to share with how to get system MAC address using C#. In C# you can get it various ways here I have discussed two different ways.

First way:

You need to import the System.Net namespace for this to work. This will support IPv4 and IPv6.

public string GetMACAddress()
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    String sMacAddress = string.Empty;
    foreach (NetworkInterface adapter in nics)
        if (sMacAddress == String.Empty)// only return MAC Address from first card  
            IPInterfaceProperties properties = adapter.GetIPProperties();
            sMacAddress = adapter.GetPhysicalAddress().ToString();
    } return sMacAddress;

Second way:

To implement this function you need to add System.Management namespace in your application. If it is not available you need to add its reference from "Add references" from your project. 

public string GetMACAddress()
    ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMOS.Get();
    string MACAddress = String.Empty;
    foreach (ManagementObject objMO in objMOC)
        if (MACAddress == String.Empty) // only return MAC Address from first card   
            MACAddress = objMO["MacAddress"].ToString();
    MACAddress = MACAddress.Replace(":", "");
    return MACAddress;

You might want to also add  "objMO ["Caption"].ToString()" to test before the mac address so that you know which devices you are looking at. 

Also, don't forget to use try/catch blocks on these as a NullPointerReference will kill the app.

How to get Client MAC address(Web):

To get the client MAC address only way we can rely on JavaScript and Active X control of Microsoft.It is only work in IE if Active X enable for IE. As the ActiveXObject is not available with the Firefox, its not working with the firefox and is working fine in IE.

This script is for IE only:

<script language="javascript" type="text/javascript">
    function showMacAddress() {
        var obj = new ActiveXObject("WbemScripting.SWbemLocator");
        var s = obj.ConnectServer(".");
        var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
        var e = new Enumerator(properties);
        var output;
        output = '<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
        output = output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
        while (!e.atEnd()) {
            var p = e.item();
            if (!p) continue;
            output = output + '<tr bgColor="#FFFFFF">';
            output = output + '<td>' + p.Caption; +'</td>';
            output = output + '<td>' + p.MACAddress + '</td>';
            output = output + '</tr>';
        output = output + '</table>';
        document.getElementById("box").innerHTML = output;

Hope that it may helps other developers for rapid development. Please inform me if any one gets anything wrong in this article.