How to get Ethernet MAC address? NOT enabled MAC Address

snowycanada
snowycanada used Ask the Experts™
on
As the title, I just need Ethernet network adapter's physical address to create password. Viusal studio 2010 C#, Windows 7 professional
Had tried different way but failed.
Seems each solution just catch the enabled one.
As attached sample1, I had tried sort by spercial word, but not every ethernet adapter description has "PCI"
Attached sample2 is the way to get the enable adapter MAC address, unuseful either.

How can i get the ethernet adapter physical address when the computer is working in wireless network?
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                if (MACAddress == String.Empty) // only return MAC Address from first card
                {
                    if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
                }
                mo.Dispose();
            }

            MACAddress = MACAddress.Replace(":", "");

Open in new window

NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
             foreach (NetworkInterface adapter in fNetworkInterfaces)
             {
                 string strDescription = adapter.Description.ToString();
                 //the MAC address which has "PCI" and not has "Wireless" is ethernet MAC
                 if (strDescription.Contains("PCI") && !strDescription.Contains("Wireless"))
                 {
                     strMAC = adapter.GetPhysicalAddress().ToString();
                 }
             }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
//Namespace reference
using System.Management;
 
/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
    //create out management class object using the
    //Win32_NetworkAdapterConfiguration class to get the attributes
    //af the network adapter
    ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
    //create our ManagementObjectCollection to get the attributes with
    ManagementObjectCollection objCol = mgmt.GetInstances();
    string address = String.Empty;
    //loop through all the objects we find
    foreach (ManagementObject obj in objCol)
    {
        if (address == String.Empty)  // only return MAC Address from first card
        {
            //grab the value from the first network adapter we find
            //you can change the string to an array and get all
            //network adapters found as well
            if ((bool)obj["IPEnabled"] == true) address = obj["MacAddress"].ToString();
        }
        //dispose of our object
        obj.Dispose();
    }
    //replace the ":" with an empty space, this could also
    //be removed if you wish
    address = address.Replace(":", "");
    //return the mac address
    return address;
}

Top Expert 2011
Commented:
static string GetMacAddress()
{
  string macAddresses = "";
  foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  {
    // Only consider Ethernet network interfaces, thereby ignoring any
    // loopback devices etc.
    if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
    if (nic.OperationalStatus == OperationalStatus.Up)
    {
      macAddresses += nic.GetPhysicalAddress().ToString();
      break;
    }
  }
  return macAddresses;
}

Author

Commented:
di_alik, you are experts

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial