winform定制一个代码段 输出运行设备的mac码此方法获取的是运行设备上的物理网卡的MAC地址,并不包括虚拟网卡或无线网卡的MAC地址。当设备具有多个网卡时,它只返回第一个正常运行的网卡的MAC地址。如果未找到任何网卡,则返回"未找到MAC地址"。windows
private static string GetMacAddress()
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
// 排除虚拟网卡和无效网卡
if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback || networkInterface.OperationalStatus != OperationalStatus.Up)
continue;
PhysicalAddress physicalAddress = networkInterface.GetPhysicalAddress();
byte[] bytes = physicalAddress.GetAddressBytes();
string macAddress = BitConverter.ToString(bytes);
return macAddress;
}
return "未找到MAC地址";
}