Click here to Skip to main content
12,954,601 members (60,323 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
How can i get MAC ID of system
Posted 19-Apr-12 3:22am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this:
private string GetMac()
{
    string Mac = string.Empty;
    ManagementClass MC = new ManagementClass("Win32_NetworkAdapter");
    ManagementObjectCollection MOCol = MC.GetInstances();
    foreach (ManagementObject MO in MOCol)
        if (MO != null)
        {
           if (MO["MacAddress"] != null)
                    {
                         Mac = MO["MACAddress"].ToString();
                         if (Mac != string.Empty)
                             break;
                    }
        }
    return Mac;
}
  Permalink  
Comments
Prasad_Kulkarni 23-May-12 9:35am
   
Glad it helps!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,
following is the code-

returns the NIC with the fastest speed that also has a valid Mac Address.
private string GetMacAddress()
    {
        const int MIN_MAC_ADDR_LENGTH = 12;
        string macAddress = "";
        long maxSpeed=-1;
 
        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            log.Debug("Found MAC Address: " + nic.GetPhysicalAddress().ToString() + " Type: " + nic.NetworkInterfaceType );
            string tempMac = nic.GetPhysicalAddress().ToString();
            if (nic.Speed > maxSpeed && !String.IsNullOrEmpty(tempMac) && tempMac.Length >= MIN_MAC_ADDR_LENGTH )
            {
                log.Debug("New Max Speed = " + nic.Speed + ", MAC: " + tempMac );
                maxSpeed = nic.Speed;
                macAddress = tempMac;
            }
        }
        return macAddress;
    }



Just returns the first one.

private string GetMacAddress()
    {
        string macAddresses = "";
 
        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }
        return macAddresses;
    }
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2017
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