Get MAC Address of Network Card using WMI and C#


Article is aimed about how to get Media Access Control Address (MAC) that is unique identifies for the Network Cards.

MAC addresses are assigned by card manufacturers when they are built.

There are variety of ways to get it but by using WMI we can get it easily and without much hassle about parsing from some source etc .

ethernet_mac_address.jpg

 


Here is how we can see mac address using cmd

>  Getmac

8-11-2010 8-15-37 PM.gif

We can get mac address from cmd also by creating process and parse the result it display but its some error prone way so let's do it perfectly using WMI way

To use WMI we need to add reference to System.Management library so add reference to that by

Project Menu >>Add reference

Now Let's setup form like below and do code like below

8-11-2010 8-26-18 PM.gif

And Write code like below in code behind of Controls

  private void Form1_Load(object sender, EventArgs e)

        {

            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter Where AdapterType='Ethernet 802.3'");

            foreach (ManagementObject mo in mos.Get())

            {

                comboBox1.Items.Add(mo["Name"].ToString());

            }

          

        }

private void btnGetMac_Click(object sender, EventArgs e)

        {

            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter where Name='"+comboBox1.SelectedItem.ToString()+"'");

            ManagementObjectCollection moc = mos.Get();

            if (moc.Count > 0)

            {

                foreach (ManagementObject mo in moc)

                {

                    textBox1.Text = (string)mo["MACAddress"];

                }

            }

          }

 

      

Here is our result  : )

8-11-2010 8-09-36 PM.gif

Thank you :)


Up Next
    Ebook Download
    View all
    Learn
    View all