C# 无法获取系统的实际MAC地址
我正在使用C# 无法获取系统的实际MAC地址,c#,asp.net,mac-address,C#,Asp.net,Mac Address,我正在使用NetworkInterface类获取系统的MAC地址,下面是代码 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { // Only consider Ethernet network interfaces, thereby ignoring any // loopback devices etc.
NetworkInterface
类获取系统的MAC地址
,下面是代码
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// Only consider Ethernet network interfaces, thereby ignoring any
// loopback devices etc.
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
nic.OperationalStatus == OperationalStatus.Up)
macAddresses += nic.GetPhysicalAddress().ToString();
}
在我的系统上,我得到的是我的系统的
MAC地址
,但在其他系统上,我得到的是我的隧道适配器物理地址00-00-00-00-00-00-00-E0,而无法得到系统的实际MAC地址
。您能帮我找出问题所在,并说明原因吗。您可以看到系统可能有多个MAC地址,因此您无法获得系统MAC地址。您可以获取特定接口的MAC地址。所以这个隧道适配器是以太网适配器,并且已经启动,所以您必须将过滤范围缩小到您想要获取地址的接口
因此,您还必须按名称
或Id
进行筛选,或者您必须使用MAC地址列表并删除任何看起来像无效值“00-00-00-00-00-00-E0”的内容
不是按名称或id列出mac地址,而是按网络接口列出:
有一个名为Name
的字段和一个名为Id
的字段,因此:
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
nic.OperationalStatus == OperationalStatus.Up && !nic.Name.Contains("Tunnel"))
macAddresses += nic.GetPhysicalAddress().ToString();
我将如何过滤他们的ID或名称的mac地址,你能给我提供这个代码。更新的答案,检查我提供的MSDN链接。过滤掉隧道接口!nic.Name.Contains(“隧道”)也可以添加!包含(“VirtualBox”)以筛选出VirtualBox适配器。