how to get MAC address using C#

How to get the MAC address of  system using

Now I want to share with you some simple but important for our rapid development.
Here I want to discuss how to get system MAC address using c# ,

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

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
        IPInterfaceProperties properties = adapter.GetIPProperties();

Second way:
public string GetMACAddress()
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection moc = mc.GetInstances();
  string MACAddress = String.Empty;
 foreach (ManagementObject mo in moc)
     if (MACAddress == String.Empty) // only return MAC Address from first card   
              if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();

  MACAddress = MACAddress.Replace(":", "");
 return MACAddress;

You might want to also add  "mo["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">
     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>';

        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>';

Hope that it may help you.Please inform me if any one get any wrong with me.


kartik said…
good article
Madhu said…
This works only with windows how abt the other OS?
Asher Frank said…
Getting the MAC deal with via the system bundle is likely only going to be legitimate on the same system section routers in between will change the MAC deal with.

Popular posts from this blog

How can I get a value of a property from an anonymous type (C#)

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."