Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi

this is code i am using for getting the macc address.
it will returnes the maccaddress of adapter ,means which one is enabled
so i need only the ethernet adapter for maccaddress


public string GetMACAddress()
       {
           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(":", "");
           return MACAddress;

       }



please specify that hint
Posted
Comments
Manuel Castillo 14-Mar-18 10:51am    
i have a doubt cause i can't use this class or component ManagementClass.. so i don't know if i missing something..

I'm not sure what the problem is.

Your code fetches the MAC address of the first enabled network adapter and strips out the semicolons. Is this not what you are expecting?

Cheers.
 
Share this answer
 
Comments
[no name] 25-Aug-11 12:46pm    
from that code i am getting the enable adapter mac address,
but i need to get the ethernet wired adapter's maccaddress ,
If the NIC card isn't currently enabled, you won't be able to see it with WMI.
 
Share this answer
 
You may have a problem with the way you think Windows networking works.

A machine can have multiple Ethernet adapters enabled all at once and they will all have their own MAC addresses. There is no "one" MAC address for the machine.

Also, if you're trying to use the MAC address for some security reason, don't. The "first" MAC you get back may not be the same "first" MAC address you got before. There is no guarantee of adapter configuration order, no guarantee that the same adapters will still be enabled, and some adapters (mostly laptop) will let you change the MAC address through the devices property page.
 
Share this answer
 
Comments
gggustafson 25-Aug-11 22:02pm    
Dave, are you sure that you can change a MAC address? Changing an IP adress I'll buy, but the MAC address is set by the NIC's manufacturer and is unique. At least I thought so.

Gus
Dave Kreskowiak 25-Aug-11 22:34pm    
Yep. I'm sure. Most desktop adapters (that I've seen) won't let you change the MAC, but on most of the laptops I've used, it's possible.
gggustafson 25-Aug-11 23:52pm    
I reread the MAC address article on Wikipedia and found that what you said was true (not doubting you, just different from what I had always thought). Great way to spoof a network. Thanks for the info.

Gus
Dave Kreskowiak 26-Aug-11 8:00am    
Not really. A MAC is only means anything on the network segment that it's on. If you try to ARP an IP address of a remote machine, you'll get the MAC of the router interface closest to you on that route.

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