C#获取 mac

这篇博客介绍了如何在C#中获取计算机的MAC地址。通过`IPGlobalProperties`类和`NetworkInterface`类,可以获取所有网络接口的信息,并过滤出以太网接口的物理地址作为MAC地址。
摘要由CSDN通过智能技术生成

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_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值