SIGN UP MEMBER LOGIN:    
ARTICLE

Get MAC Address of Network Card using WMI and C#

Posted by Kirtan Patel Articles | Learn .NET August 11, 2010
Get MAC Address of Network Cards using WMI and C#
Reader Level:
Download Files:
 

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 :)


Login to add your contents and source code to this article
share this article :
post comment
 

This code is not run on the web? I can't get Mac on my computer with aspx.net

Posted by Hoang Nhi Nov 28, 2011

Hi, I'm currently using Visual Studio 2008 C#. An error came up when I copy and pasted the code in the right part of my code. The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?) Any help??

Posted by Aiden Byfield Mar 01, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor