14 Mart 2016 Pazartesi

C#.net - Get MAC Address

C#.net - Get MAC Address 


Sometime in our application we need to fetch MAC Address of network adapter card.In this article i will show you how to get a MAC Address.


Step 1
Create a Console Application and give the solution name as  ConMACAddress.


Step 2
Add System.Management assembly reference to the project from solution explorer,it is look like this





Click on image for better view


Step 3
Write a static method for get MAC Address,it is look like this


  1. public static String GetMACAddress()  
  2.        {  
  3.            #region Get MAC Address  
  4.            try  
  5.            {  
  6.                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  7.                ManagementObjectCollection moc = mc.GetInstances();  
  8.                string MACAddress = String.Empty;  
  9.                foreach (ManagementObject mo in moc)  
  10.                {  
  11.                    if (MACAddress == String.Empty) // only return MAC Address from first card  
  12.                    {  
  13.                        if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();  
  14.                    }  
  15.                    mo.Dispose();  
  16.                }  
  17.   
  18.                MACAddress = MACAddress.Replace(":""");  
  19.                return MACAddress;  
  20.            }  
  21.            catch (Exception ex)  
  22.            {  
  23.                throw new Exception(ex.Message);  
  24.            }  
  25.            #endregion  
  26.        }  

Step 4
Call above function in main method,it is look like this

  1. static void Main(string[] args)  
  2.         {  
  3.             try  
  4.             {  
  5.                 System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());    
  6.             }  
  7.             catch (Exception ex)  
  8.             {  
  9.                 System.Console.WriteLine(ex.Message);     
  10.             }  
  11.         }  

Run the Project.


Full Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Management;  
  6.   
  7. namespace ConMACAddress  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             try  
  14.             {  
  15.                 System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());    
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 System.Console.WriteLine(ex.Message);     
  20.             }  
  21.         }  
  22.   
  23.         public static String GetMACAddress()  
  24.         {  
  25.             #region Get MAC Address  
  26.             try  
  27.             {  
  28.                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  29.                 ManagementObjectCollection moc = mc.GetInstances();  
  30.                 string MACAddress = String.Empty;  
  31.                 foreach (ManagementObject mo in moc)  
  32.                 {  
  33.                     if (MACAddress == String.Empty) // only return MAC Address from first card  
  34.                     {  
  35.                         if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();  
  36.                     }  
  37.                     mo.Dispose();  
  38.                 }  
  39.   
  40.                 MACAddress = MACAddress.Replace(":""");  
  41.                 return MACAddress;  
  42.             }  
  43.             catch (Exception ex)  
  44.             {  
  45.                 throw new Exception(ex.Message);  
  46.             }  
  47.             #endregion  
  48.         }  
  49.     }  
  50. }  

Download
Download Source Code