Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法获取系统的实际MAC地址_C#_Asp.net_Mac Address - Fatal编程技术网

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适配器。