C # obtain the MAC address
Method 1: Use managementclass
String Strmac = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject MOC in MoC)
{
If (MOC ["ipenabled"]. tostring () = "true ")
{
Strmac = MOC ["macaddress"]. tostring ();
}
}
Method 2: Use networkinterface
String Strmac = "";
Networkinterface [] fnetworkinterfaces = networkinterface. getallnetworkinterfaces ();
Foreach (networkinterface adapter in fnetworkinterfaces)
{
Strmac = Adapter. getphysicaladdress (). tostring ();
}
If your computer has both a wireless network card and a wired network card, and you have a virtual MAC address when using broadband, You need to obtain the local wired network card address, it is necessary to distinguish MAC addresses, combined with ipconfig/all in cmd
Command content
Ethernet Adapter local connection:
Connection-specific DNS suffix .:
Description ......: RealTek PCIe GBE family Controller
Physical address ......: 00-26-9e-8b-a9-f2
DHCP enabled...
IP address ......: 192.168.9.123
Subnet Mask ......: 255.255.255.0
Default Gateway ......: 192.168.9.1
Ethernet Adapter wireless network connection:
Media State ......: Media disconnected
Description ......: 11b/G/n Wireless LAN mini-PCI Express adapter II
Physical address...: 70 -1a-04-51-4d-5e
PPP adapter broadband connection:
Connection-specific DNS suffix .:
Description ......: Wan (PPP/slip) Interface
Physical address...: 00-53-45-00-00-00
DHCP enabled...
IP address ......: 117.93.142.100
Subnet Mask ......: 255.255.255.255
Default Gateway ......: 117.93.142.100
DNS servers ......: 61.177.7.1
221.228.255.1
NetBIOS over TCPIP ......: Disabled
It is found that Mac can be distinguished in the description of the connection.
Differentiation Condition
If (adapter. description. Contains ("PCI ")&&! Adapter. description. Contains ("wireless "))
// Networkinterface
// This condition can be used to determine the MAC address of the wired Nic.
If (strdescription. Contains ("PCI ")&&! Strdescription. Contains ("wireless "))
// Managementclass
// This condition can be used to determine the MAC address of the wired Nic.
You can also compare the three connection names (local connection, wireless network connection, and broadband connection), but this is not the name of the connection on each machine, so it is not reliable to use this as the condition!
I think someone reads the Registry to distinguish between a wireless network card and a wired network card by reading the key value in the Registry Nic information and whether it contains "PCI". What I want to say here is, wireless NICs also have PCI.
Ethernet Adapter wireless network connection:
Media State ......: Media disconnected
Description ......: 11b/G/n Wireless LAN mini-PCI Express adapter II
Physical address...: 70 -1a-04-51-4d-5e