User Id :    Password :      New Member   Forgot Password  
 
How to get MAC address using CSharp
Description In this article 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# ,   No. of Views     1720
  Rating     0
Author Ahsan Murshed   Posted On     15 Jul 2010
Tags ASP.NET,C#,Java Script,HTML    

Sample Code   Download Code

How to get the MAC address of system using Asp.net/C#

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();
        adapter.GetPhysicalAddress().ToString();
    }

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();
           }
      mo.Dispose();
 }

  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:




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

About Author

About Author I am working as Software engineer having 4+ years of experience in Microsoft Technologies, System Design and Integration and deployment, Search Engine Optimization (SEO). I have developed web based application in asp.net/C# and database design. My Site: http://www.dotnetboss.com My blog: http://aspboss.blogspot.com Ahsan
No Photo
 
Country N/A
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code