How to get MAC Address of Network Card using WMI and C#

No.of Views3502
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  09 Sep 2010 07:09:29
Tag : CSharp , WMI
In this article, i have demonstrate to you, how to get MAC Address of Network Cards using WMI and C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

The MAC Address in PC

Image Loading

First i would show , how we can see mac address using command prompt 

Image Loading

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.in order 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

 

Image Loading

Code

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"];
                }
            }
          }

 Output 

Image Loading

I hope this is help you.happy coding.

Sample Project Source

Download source files -41 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top