如何在asp.net项目中获取MacAddress地址

  介绍

这篇文章将为大家详细讲解有关如何在asp.net项目中获取MacAddress地址,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

因为在dotnetcore是没有直接和硬件相关的,所以无法通过WMI的方法获取当前设备的Mac地址

但是在dotnet核心可以使用下面的代码拿到本机所有的网卡地址,包括物理网卡和虚拟网卡

IPGlobalProperties  computerProperties =, IPGlobalProperties.GetIPGlobalProperties ();   ,,NetworkInterface [], nics =, NetworkInterface.GetAllNetworkInterfaces ();      ,,Console.WriteLine (“Interface 方式;for  {0}, {1},,,,   ,,,,,,computerProperties.HostName computerProperties.DomainName);   ,,if  (nics ==, null  | |, nics.Length  & lt;, 1)   ,,{   ,,,Console.WriteLine (“, No  network  interfaces 发现!”);   ,,,返回;   ,,}      ,,Console.WriteLine (“, Number  of  interfaces  ....................,:, {0},,, nics.Length);   ,,foreach  (NetworkInterface  adapter  nic拷贝)   ,,{   ,,,Console.WriteLine ();   ,,,Console.WriteLine(时间+ adapter.Name “,“, +, adapter.Description);   ,,,Console.WriteLine (String.Empty.PadLeft (adapter.Description.Length, & # 39;=& # 39;));   ,,,Console.WriteLine (“, Interface  type  ..........................,:, {0},,, adapter.NetworkInterfaceType);   ,,,Console.Write (“, Physical  address  ........................,:,“);   ,,,PhysicalAddress  address =, adapter.GetPhysicalAddress ();   ,,,byte [], bytes =, address.GetAddressBytes ();   ,,,for  (int 小姐:=,0;,小姐:& lt;, bytes.Length;,我+ +)   ,,,{   ,,,,//,Display 从而physical  address 拷贝十六进制。   ,,,,Console.Write(“{0}“,,字节[我].ToString (“X2"));   ,,,,//,Insert  a  hyphen  after  each 字节,,unless 断开连接;at 我方表示歉意,并最终获得of 从而   ,,,,//地址。   ,,,,if (小姐:!=,bytes.Length 作用;1)   ,,,,{   ,,,,,Console.Write(“产生绯闻);   ,,,,}   ,,,}      ,,,Console.WriteLine ();   以前,,}

运行代码,下面是控制台

Interface 方式、for  lindexi.github   ,,,Number  of  interfaces  ....................,: 6      ,,,Hyper-V  Virtual  Ethernet  Adapter  # 4   ,,,===================================,,,Interface  type  ..........................,:以太网   ,,,Physical  address  ........................,:, 00 - 15 - 5 d - 96 - 39 - 03      ,,,Hyper-V  Virtual  Ethernet  Adapter  # 3   ,,,===================================,,,Interface  type  ..........................,:以太网   ,,,Physical  address  ........................,:, 1 c - 1 b - 0 - d - 3 - c - 47 - 91      ,,,Software  Loopback  Interface  1   ,,,=============================,,,Interface  type  ..........................,:回送   ,,,Physical  address  ........................,:      ,,,Microsoft  Teredo  Tunneling 适配器   ,,,==================================,,,Interface  type  ..........................,:隧道   ,,,Physical  address  ........................,: 00-00-00-00-00-00-00-E0      ,,,Hyper-V  Virtual  Ethernet 适配器   ,,,================================,,,Interface  type  ..........................,:以太网   ,,,Physical  address  ........................,:, 5 - 15 - 31 - 73 - b0 - 9 - f      ,,,Hyper-V  Virtual  Ethernet  Adapter  # 2   ,,,===================================,,,Interface  type  ..........................,:以太网   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在asp.net项目中获取MacAddress地址