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

11 comments:

  1. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  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