为什么我尝试获取 MAC 地址时得到的是空字符串?
Why am I getting an empty string when I try to get my MAC address?
我正在尝试以编程方式获取系统的 MAC 地址。我正在用 C# 编写,在 Windows 10 OS 上使用 Visual Studios .NET 框架。我已经包含了 System.Net.NetworkInformation 命名空间(对于像我这样的其他 .NET 新手,必须手动将其添加到解决方案资源管理器下的 "References" 部分,然后使用 "using" 关键字)。要获取 MAC 地址,我使用以下代码:
try
{
var macAddr =
(
from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();
Console.WriteLine("MAC address is: {0}", macAddr);
}
catch (Exception e)
{
Console.WriteLine("Could not collect MAC Address;\nERROR: {0}", e);
}
我期望的输出是:
Attempting to collect MAC Address...
MAC address is: XXXXXXXXXXXX
其中 XXX.... 是 12 个字符的 MAC 地址,或者:
Attempting to collect MAC Address...
Could not collect MAC Address;
ERROR: [error report inserted here]
然而,我得到的是:
Attempting to collect MAC Address...
MAC address is:
其中 macAddr 变量似乎收集了一个空字符串。
所以...我的问题是,为什么我得到一个明显的空字符串而不是我期望的 12 个字符的 MAC 地址?
您正在获取 Software Loopback Interface 1
的空 MAC 地址。
在尝试 运行 此代码之前,请确保您已与网络建立有效连接。
活动意味着接口已激活(启动)并连接到具有 DHCP 给定 IP 地址或静态 IP 地址的网络。
要解决此问题,您只需通过
从查询中删除所有 Loopback Interfaces
var macAddresses = from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
select nic.GetPhysicalAddress().ToString();
编辑,无论您的网卡 OperationalStatus
是什么,您都可以进行检查,对于非空 MAC 地址,只需 "ask" 即可:
var macAddresses = from nic in NetworkInterface.GetAllNetworkInterfaces()
where !string.IsNullOrEmpty(nic.GetPhysicalAddress().ToString())
select nic.GetPhysicalAddress().ToString();
有许多类型的网络适配器没有物理地址,例如 tunel
或 Loopback
过滤到 Ethernet
类型:
where nic.OperationalStatus == OperationalStatus.Up &&
nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet
我正在尝试以编程方式获取系统的 MAC 地址。我正在用 C# 编写,在 Windows 10 OS 上使用 Visual Studios .NET 框架。我已经包含了 System.Net.NetworkInformation 命名空间(对于像我这样的其他 .NET 新手,必须手动将其添加到解决方案资源管理器下的 "References" 部分,然后使用 "using" 关键字)。要获取 MAC 地址,我使用以下代码:
try
{
var macAddr =
(
from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();
Console.WriteLine("MAC address is: {0}", macAddr);
}
catch (Exception e)
{
Console.WriteLine("Could not collect MAC Address;\nERROR: {0}", e);
}
我期望的输出是:
Attempting to collect MAC Address...
MAC address is: XXXXXXXXXXXX
其中 XXX.... 是 12 个字符的 MAC 地址,或者:
Attempting to collect MAC Address...
Could not collect MAC Address;
ERROR: [error report inserted here]
然而,我得到的是:
Attempting to collect MAC Address...
MAC address is:
其中 macAddr 变量似乎收集了一个空字符串。
所以...我的问题是,为什么我得到一个明显的空字符串而不是我期望的 12 个字符的 MAC 地址?
您正在获取 Software Loopback Interface 1
的空 MAC 地址。
在尝试 运行 此代码之前,请确保您已与网络建立有效连接。
活动意味着接口已激活(启动)并连接到具有 DHCP 给定 IP 地址或静态 IP 地址的网络。
要解决此问题,您只需通过
从查询中删除所有Loopback Interfaces
var macAddresses = from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
select nic.GetPhysicalAddress().ToString();
编辑,无论您的网卡 OperationalStatus
是什么,您都可以进行检查,对于非空 MAC 地址,只需 "ask" 即可:
var macAddresses = from nic in NetworkInterface.GetAllNetworkInterfaces()
where !string.IsNullOrEmpty(nic.GetPhysicalAddress().ToString())
select nic.GetPhysicalAddress().ToString();
有许多类型的网络适配器没有物理地址,例如 tunel
或 Loopback
过滤到 Ethernet
类型:
where nic.OperationalStatus == OperationalStatus.Up &&
nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet