Link to home
Create AccountLog in
Avatar of Steven Xing
Steven XingFlag for Canada

asked on

How to get Ethernet MAC address? NOT enabled MAC Address

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

SOLUTION
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Steven Xing

ASKER

di_alik, you are experts