/// <summary>
/// 获取电脑 MAC地址
/// </summary>
/// <param name="separator"></param>
/// <returns></returns>
public static List<string> GetActiveMacAddress(string separator = "-")
{
//本地计算机网络连接信息
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
//获取本机电脑名
var HostName = computerProperties.HostName;
//获取域名
var DomainName = computerProperties.DomainName;
//获取本机所有网络连接
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
var macAddress = new List<string>();
if (nics == null || nics.Length < 1)
{
//Debug.WriteLine(" No network interfaces found.");
return macAddress;
}
//Debug.WriteLine(" Number of interfaces .................... : {0}", nics.Length);
foreach (NetworkInterface adapter in nics.Where(c =>
c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up))
{
IPInterfaceProperties properties = adapter.GetIPProperties();
var unicastAddresses = properties.UnicastAddresses;
if (unicastAddresses.Any(temp => temp.Address.AddressFamily == AddressFamily.InterNetwork))
{
var address = adapter.GetPhysicalAddress();
if (string.IsNullOrEmpty(separator))
{
macAddress.Add(address.ToString());
}
else
{
var MACIp = "";
byte[] bytes = address.GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
MACIp += bytes[i].ToString("X2");
if (i != bytes.Length - 1)
{
MACIp += separator;
}
}
macAddress.Add(MACIp);
}
}
}
return macAddress;
}