工作笔记20230328.
在C#中,可以使用NetworkInterface类来获取本地计算机的MAC地址。
using System.Net.NetworkInformation;
public string GetMACAddress()
{
string macAddress = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddress = nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddress;
}
这个方法首先获取本地计算机上所有网络接口,然后在这些接口中找到处于操作状态的接口(即连接着网络的接口),并返回该接口的物理地址(即MAC地址)。注意,如果计算机上没有连接到网络的接口,则该方法将返回空字符串。
把mac地址加上分隔符:
string macAddress = "1125565C00001";
macAddress = macAddress.Insert(2, ":").Insert(5, ":").Insert(8, ":").Insert(11, ":").Insert(14, ":");
Console.WriteLine(macAddress); //