Saturday, June 18, 2011

C#.net - Get MAC Address through C#.net

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


 public static String GetMACAddress()
        {
            #region Get MAC Address
            try
            {
                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;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            #endregion
        }

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

static void Main(string[] args)
        {
            try
            {
                System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());  
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);   
            }
        }

Run the Project.


Full Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ConMACAddress
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());  
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);   
            }
        }

        public static String GetMACAddress()
        {
            #region Get MAC Address
            try
            {
                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;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            #endregion
        }
    }
}


Download
Download Source Code

10 comments:

  1. thanx kishor naik ji...
    this help me alot...

    ReplyDelete
  2. not working managmentclass error and also in ManagementObjectCollection.........

    ReplyDelete
    Replies
    1. Did you add reference System.Management Library????

      Delete
  3. Thanks for sharing this code i was asked in an interview that What is the procedure to get MAC Address using C#.net ? .

    Regards
    Johnny Depp

    Make Money Online


    ReplyDelete
  4. Works great for a PC but how to capture for Apple OS?

    ReplyDelete
  5. Hey I need a help. I have a wordpress blog where I want to track the geographic locations of the visitors. I have heard the answer that it's not possible but still I am searching for a way. You got any ideas friend?

    ReplyDelete