public String GetMacAddress()
{//没有缓存的地址,则查询
String mac_s = “”;
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
if (nics == null || nics.Length < 1)
{
return mac_s;
}
try
{
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties(); // .GetIPInterfaceProperties();
string Description = adapter.Description;//接口的描述
string NetworkInterfaceType = adapter.NetworkInterfaceType.ToString();//获取接口类型
PhysicalAddress address = adapter.GetPhysicalAddress();//返回MAC地址
if (NetworkInterfaceType != “Loopback” && NetworkInterfaceType == “Ethernet”)
mac_s = adapter.GetPhysicalAddress().ToString();
}
} catch (Exception ) {; }
return mac_
C#获取 mac
最新推荐文章于 2024-08-22 13:57:13 发布
这篇博客介绍了如何在C#中获取计算机的MAC地址。通过`IPGlobalProperties`类和`NetworkInterface`类,可以获取所有网络接口的信息,并过滤出以太网接口的物理地址作为MAC地址。
摘要由CSDN通过智能技术生成