Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can any one provide me the snippet to get the MAC Address.

Right now I tried a lot of snippet but they all not worked well

Snippet-1 :
C#
public static string GetMacAddress()
{
    string macAddresses = "";

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }
    return macAddresses;
}


It returns empty string in my system

Snippet-2 :
C#
public static 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;
        }


It also returns empty string

Snippet-3 :
C#
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;
}


It only works for Ethernet network, mine is ppp

Can any one provide me snippet to find MAC Address which works for every computer

Thanks In Advance
Posted
Comments
Kornfeld Eliyahu Peter 23-Mar-15 15:56pm    
Not all network interfaces has MAC address, only those has a physical network card behind it...
LGM-Developer 23-Mar-15 16:11pm    
Remove the reference to the NetworkInterfaceType

nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet

That snippet will now work...

BTW a PPPoE interface does have a MAC Address... In fact all ethernet devices have an address...

A network packet holds the source and destination of the packet... in the example below ff:ff:ff:ff:ff:ff is the broadcast mac address.

Frame 1 (44 bytes on wire, 44 bytes captured)
Ethernet II, Src: 00:50:da:42:d7:df, Dst: ff:ff:ff:ff:ff:ff
PPP-over-Ethernet Discovery
Version: 1
Type 1
Code Active Discovery Initiation (PADI)
Session ID: 0000
Payload Length: 24
PPPoE Tags
Tag: Service-Name
Tag: Host-Uniq
Binary Data: (16 bytes)
Dave Kreskowiak 23-Mar-15 18:25pm    
First, what kind of app are you writing, ASP.NET, Windows Forms, ...? What are you using the MAC address for?
binadi007 24-Mar-15 11:16am    
WPF Application. Is Processor ID Unique?

1 solution

C#
using System;
using System.Linq;
using System.Net.NetworkInformation;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            foreach (var nic in NetworkInterface.GetAllNetworkInterfaces().Where(nic => nic.OperationalStatus == OperationalStatus.Up)) {
                Console.WriteLine("{0} has an address of {1}", nic.NetworkInterfaceType, nic.GetPhysicalAddress());
            }
            Console.ReadKey();
        }
    }
}


This console app returns all the interfaces that are up...

C#
private static string[] GetUpInterfaces()
        {
            return
                NetworkInterface.GetAllNetworkInterfaces()
                    .Where(nic => nic.OperationalStatus == OperationalStatus.Up)
                    .Select(xx => xx.GetPhysicalAddress().ToString()).ToArray();
        }
 
Share this answer
 

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