Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
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 22-Apr-12 4:00am
turjoy314
Edited 22-Apr-12 5:31am
Nelek63.4K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you mean MAC address, then:
 
        /// <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:
            MessageBox.Show(GetMacAddress().ToString());
  Permalink  
Comments
Member 8026992 at 22-Dec-12 7:36am
   
Not Working GetMacAddress return Always null
OriginalGriff at 22-Dec-12 8:00am
   
So use the debugger and check what NetworkInterface.GetAllNetworkInterfaces returns.
Usha Sanjee at 19-Jun-14 7:20am
   
Its working ..:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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);
  Permalink  
Comments
vishaltn at 4-Apr-13 3:09am
   
What class does the NetWorkInterface belong ? What should i import ?
Arigbede Moses at 10-Apr-13 4:27am
   
System.Net.NetworkInformation.NetworkInterface
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

From System.Net namespace:
 
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;
}
  Permalink  
Comments
Dinesh92 at 11-Nov-13 1:36am
   
awesome code....i got the address...thanks buddy
Eng Mahmoud Gamal at 17-Sep-14 10:02am
   
you are welcome ^_^
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

From WMI
 
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;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5


using system.data;
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 3 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100