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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/CPFlying/archive/2010/03/14/1685733.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值