C# 区分无线网卡和有线网卡的MAC

C# 获取MAC地址

 

方法一: 使用 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();
        }
}

 

方法二:使用NetworkInterface

 

string strMAC="";
NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in fNetworkInterfaces)
{
    strMAC = adapter.GetPhysicalAddress().ToString();

 

}

 

 

 

如果你的电脑中既有无线网卡,也有有线网卡,而且在你使用宽带的时候还会有一个虚拟MAC地址,这时要想获取本地的有线网卡地址,就要实现MAC地址的区分,结合CMD下ipconfig /all

 

命令的内容

 

Ethernet adapter 本地连接:

 

 

 

        Connection-specific DNS Suffix  . :

 

        Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller

 

        Physical Address. . . . . . . . . : 00-26-9E-8B-A9-F2

 

        Dhcp Enabled. . . . . . . . . . . : No

 

        IP Address. . . . . . . . . . . . : 192.168.9.123

 

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

 

        Default Gateway . . . . . . . . . : 192.168.9.1

 

 

 

Ethernet adapter 无线网络连接:

 

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 宽带连接:

 

 

 

        Connection-specific DNS Suffix  . :

 

        Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface

 

        Physical Address. . . . . . . . . : 00-53-45-00-00-00

 

        Dhcp Enabled. . . . . . . . . . . : No

 

        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

 

 

发现可以在连接的Description中区分MAC

 

区分条件

 

 if(adapter.Description.Contains("PCI") && !adapter.Description.Contains("Wireless"))

 

 

// NetworkInterface

 

// 这个条件可以判断出有线网卡的MAC

 

 

if (strDescription.Contains("PCI")&&!strDescription.Contains("Wireless"))

 

 

// ManagementClass

 

// 这个条件可以判断出有线网卡的MAC

 

 

 

也可以比较3个连接名称(本地连接,无线网络连接,宽带连接),但是并不是每台机上的连接名称都是这个,所以用这个作为条件不可靠!

 

我看网上有人读注册表来区分,他们是通过读取注册表网卡信息中的某个键值是否含有"PCI"来区分无线网卡和有线网卡,这里我想说的是,无线网卡也有PCI的。

 

Ethernet adapter 无线网络连接:

 

Media State . . . . . . . . . . . : Media disconnected

 

Description . . . . . . . . . . . : 11b/g/n  Wireless LAN Mini-PCI Express Adapter II

 

Physical Address. . . . . . . . . : 70-1A-04-51-4D-5E

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2010-03-14 21:21  思想在飞  阅读(9337)  评论(4编辑  收藏  举报