asp.net core 获取 MacAddress 地址方法示例
本文告诉大家如何在dotnetcore获取Mac地址
因为在dotnetcore是没有直接和硬件相关的,所以无法通过WMI的方法获取当前设备的Mac地址
但是在dotnetcore可以使用下面的代码拿到本机所有的网卡地址,包括物理网卡和虚拟网卡
IPGlobalPropertiescomputerProperties=IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[]nics=NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interfaceinformationfor{0}.{1}",
computerProperties.HostName,computerProperties.DomainName);
if(nics==null||nics.Length<1)
{
Console.WriteLine("Nonetworkinterfacesfound.");
return;
}
Console.WriteLine("Numberofinterfaces....................:{0}",nics.Length);
foreach(NetworkInterfaceadapterinnics)
{
Console.WriteLine();
Console.WriteLine(adapter.Name+","+adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
Console.WriteLine("Interfacetype..........................:{0}",adapter.NetworkInterfaceType);
Console.Write("Physicaladdress........................:");
PhysicalAddressaddress=adapter.GetPhysicalAddress();
byte[]bytes=address.GetAddressBytes();
for(inti=0;i
运行代码,下面是控制台
Interfaceinformationforlindexi.github
Numberofinterfaces....................:6
Hyper-VVirtualEthernetAdapter#4
===================================
Interfacetype..........................:Ethernet
Physicaladdress........................:00-15-5D-96-39-03
Hyper-VVirtualEthernetAdapter#3
===================================
Interfacetype..........................:Ethernet
Physicaladdress........................:1C-1B-0D-3C-47-91
SoftwareLoopbackInterface1
=============================
Interfacetype..........................:Loopback
Physicaladdress........................:
MicrosoftTeredoTunnelingAdapter
==================================
Interfacetype..........................:Tunnel
Physicaladdress........................:00-00-00-00-00-00-00-E0
Hyper-VVirtualEthernetAdapter
================================
Interfacetype..........................:Ethernet
Physicaladdress........................:5A-15-31-73-B0-9F
Hyper-VVirtualEthernetAdapter#2
===================================
Interfacetype..........................:Ethernet
Physicaladdress........................:5A-15-31-08-13-B1
但是可以看到里面有很多不需要使用的网卡,从堆栈网找到的方法获取当前有活跃的ip的网卡可以通过先判断是不是本地巡回网络等,然后判断有没有网络
foreach(NetworkInterfaceadapterinnics.Where(c=>
c.NetworkInterfaceType!=NetworkInterfaceType.Loopback&&c.OperationalStatus==OperationalStatus.Up))
获取当前的网卡有没ip有ip才是需要的
IPInterfacePropertiesproperties=adapter.GetIPProperties();
varunicastAddresses=properties.UnicastAddresses;
foreach(vartempinunicastAddresses.Where(temp=>
temp.Address.AddressFamily==AddressFamily.InterNetwork))
{
//这个才是需要的网卡
}
简单输出网卡使用adapter.GetPhysicalAddress().ToString()输出,如果需要输出带连接的请使用GetAddressBytes然后自己输出
下面的代码是我抽出来的,可以直接使用
publicstaticvoidGetActiveMacAddress(stringseparator="-")
{
NetworkInterface[]nics=NetworkInterface.GetAllNetworkInterfaces();
//Debug.WriteLine("Interfaceinformationfor{0}.{1}",
//computerProperties.HostName,computerProperties.DomainName);
if(nics==null||nics.Length<1)
{
Debug.WriteLine("Nonetworkinterfacesfound.");
return;
}
varmacAddress=newList();
//Debug.WriteLine("Numberofinterfaces....................:{0}",nics.Length);
foreach(NetworkInterfaceadapterinnics.Where(c=>
c.NetworkInterfaceType!=NetworkInterfaceType.Loopback&&c.OperationalStatus==OperationalStatus.Up))
{
//Debug.WriteLine("");
//Debug.WriteLine(adapter.Name+","+adapter.Description);
//Debug.WriteLine(string.Empty.PadLeft(adapter.Description.Length,'='));
//Debug.WriteLine("Interfacetype..........................:{0}",adapter.NetworkInterfaceType);
//Debug.Write("Physicaladdress........................:");
//PhysicalAddressaddress=adapter.GetPhysicalAddress();
//byte[]bytes=address.GetAddressBytes();
//for(inti=0;itemp.Address.AddressFamily==AddressFamily.InterNetwork))
{
varaddress=adapter.GetPhysicalAddress();
if(string.IsNullOrEmpty(separator))
{
macAddress.Add(address.ToString());
}
else
{
macAddress.Add(string.Join(separator,address.GetAddressBytes()));
}
}
}
}
上面的方法不仅是在dotnetcore可以使用,在dotnetframework程序同样调用,但是在dotnetframework还可以通过WMI获取
在dotnetframework使用WMI获取MAC地址方法
varmanagementClass=newManagementClass("Win32_NetworkAdapterConfiguration");
varmanagementObjectCollection=managementClass.GetInstances();
foreach(varmanagementObjectinmanagementObjectCollection.OfType())
{
using(managementObject)
{
if((bool)managementObject["IPEnabled"])
{
if(managementObject["MacAddress"]==null)
{
returnstring.Empty;
}
returnmanagementObject["MacAddress"].ToString().ToUpper();
}
}
}
输出的格式是5A:15:31:73:B0:9F同时输出是一个网卡
NetworkInterface.GetPhysicalAddressMethod(System.Net.NetworkInformation)
PhysicalAddressClass(System.Net.NetworkInformation)
c#-.NETCore2.xhowtogetthecurrentactivelocalnetworkIPv4address?-StackOverflow
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。