C # differentiate between a wireless network card and a MAC address of a wired network card

Source: Internet
Author: User

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.