Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Tip/Trick

MAC address and Network Interface Description

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
28 Jan 2014CPOL1 min read 18.3K   10   1
Finds the mac address and description of network interface

Introduction

A Console Application that shows the MAC addresses and description of that Network interface card. This is for beginners who wants a quick solution for finding their MAC address.

Background 

MAC address (media access control address) is a unique identifier assigned to network interfaces for communications on the physical network segment. MAC addresses are used as a network address for most IEEE 802 network technologies, including Ethernet. Logically, MAC addresses are used in the media access control protocol sublayer of the OSI reference model.

MAC addresses are most often assigned by the manufacturer of a network interface controller (NIC) and are stored in its hardware, such as the card's read-only memory or some other firmwaremechanism. If assigned by the manufacturer, a MAC address usually encodes the manufacturer's registered identification number and may be referred to as the burned-in address (BIA). It may also be known as an Ethernet hardware address (EHA), hardware address or physical address. This can be contrasted to a programmed address, where the host device issues commands to the NIC to use an arbitrary address.

A network node may have multiple NICs and each must have one unique MAC address per NIC. 

Reference: Wikipedia  

Using the code 

The code is very simple, where a method GetMyMacAddress() returns a list of type string. Make sure to use the namespace "System.Net.NetworkInformation" - you can loop through NetworkInterface where it has the member method GetAllNetworkInterfaces().

Physical address can be retrieved from GetPhysicalAddress(). 

C#
 private static List<string> GetMyMacAddress()
 {
     List<String> slistMac = new List<string>();

     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
     {
         PhysicalAddress address = nic.GetPhysicalAddress();
         if ((NetworkInterfaceType.Tunnel != nic.NetworkInterfaceType)
              && !String.IsNullOrEmpty(address.ToString()))
         {
             string sMac = string.Join(":", (from z in nic.GetPhysicalAddress()
                  .GetAddressBytes() select z.ToString("X2")).ToArray());
             slistMac.Add(String.Format("{0}\t {1}", sMac, nic.Description));

         }
     }
     return slistMac;
 }
  Then you can display MAC by calling GetMyMacAddress() in your main Method
C#
 foreach (string sMac in GetMyMacAddress())
            Console.WriteLine(sMac);
  

History 

This is the first version, Next could be display MAC using WPF..!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IBI Limited
United Kingdom United Kingdom
Being a human - I am a Coder, a Blogger, a Tweeter, a Son, a Brother and a human being..

Comments and Discussions

 
QuestionMethod signature Pin
JV999929-Jan-14 3:09
professionalJV999929-Jan-14 3:09 
You call your method "GetMyMacAddress()", but in reality it returns a list of all available Mac Addresses which are not tunneled, not just the primary. So I would either rename the method or change the code.

A smaller note:
Due to the list of strings you return you just get a list of mac addesses from which you do not know to which nic they belong etc.. That would mean you would need to write additional code for it. That doesn't make this method very useful in most scenarios.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.