Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.36/5 (5 votes)
See more:
how can i get my pc's MAC address using c#? i want to print my MAC address in message box... please help me..

[edit] Spelling [/edit]
Posted
Updated 28-Feb-18 1:16am
v2
Comments
Henck 8-Sep-16 3:28am    
Here you go: http://www.independent-software.com/get-local-machines-mac-address-in-c/

If you mean MAC address, then:

C#
/// <summary>
/// Gets the MAC address of the current PC.
/// </summary>
/// <returns></returns>
public static PhysicalAddress GetMacAddress()
    {
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
        // Only consider Ethernet network interfaces
        if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
            nic.OperationalStatus == OperationalStatus.Up)
            {
            return nic.GetPhysicalAddress();
            }
        }
    return null;
    }
You can then just use ToString to display the address:
C#
MessageBox.Show(GetMacAddress().ToString());
 
Share this answer
 
Comments
Member 8026992 22-Dec-12 7:36am    
Not Working GetMacAddress return Always null
OriginalGriff 22-Dec-12 8:00am    
So use the debugger and check what NetworkInterface.GetAllNetworkInterfaces returns.
Usha Sanjee 19-Jun-14 7:20am    
Its working ..:)
Manuel Castillo 13-Mar-18 14:42pm    
works perfectly
I added a little tweak in there with all due respect to OriginalGriff. This will ensure that:
1. The Adapter that is currently connected is Picked;
2. All Virtual and Pseudo Adapters will not be considered even though the are "Connected".
3. Ensure that No Null or Empty MAC is Appended.

C#
string mac = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {

                if (nic.OperationalStatus == OperationalStatus.Up && (!nic.Description.Contains("Virtual") && !nic.Description.Contains("Pseudo")))
                {
                    if (nic.GetPhysicalAddress().ToString() != "")
                    {
                        mac = nic.GetPhysicalAddress().ToString();
                    }
                }
            }
MessageBox.Show(mac);
 
Share this answer
 
Comments
vishaltn 4-Apr-13 3:09am    
What class does the NetWorkInterface belong ? What should i import ?
Arigbede Moses 10-Apr-13 4:27am    
System.Net.NetworkInformation.NetworkInterface
From System.Net namespace:

C#
public string GetMACAddress()
{
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    String sMacAddress = string.Empty;
    foreach (NetworkInterface adapter in nics)
    {
        if (sMacAddress == String.Empty)// only return MAC Address from first card
        {
            IPInterfaceProperties properties = adapter.GetIPProperties();
            sMacAddress = adapter.GetPhysicalAddress().ToString();
        }
    } return sMacAddress;
}
 
Share this answer
 
Comments
[no name] 11-Nov-13 1:36am    
awesome code....i got the address...thanks buddy
Eng Mahmoud Gamal 17-Sep-14 10:02am    
you are welcome ^_^
Filip Puškáš 5-Jun-18 9:18am    
Hi, at first thanks for this code ... I wanna know why it shows different MAC address as a solution 2, thanks
From WMI

C#
public string GetMACAddress()
{
    ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMOS.Get();
    string MACAddress = String.Empty;
    foreach (ManagementObject objMO in objMOC)
    {
        if (MACAddress == String.Empty) // only return MAC Address from first card   
        {
            MACAddress = objMO["MacAddress"].ToString();
        }
        objMO.Dispose();
    }
    MACAddress = MACAddress.Replace(":", "");
    return MACAddress;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900