Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP SQL
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 at 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
0 Sergey Alexandrovich Kryukov 265
1 _Amy 235
2 Peter Leow 205
3 Andreas Gieriet 180
4 Shmuel Zang 120
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,412
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Apr 2012
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