国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net core 獲取 MacAddress 地址方法示例

2024-07-10 12:48:34
字體:
來源:轉載
供稿:網友

本文告訴大家如何在 dotnet core 獲取 Mac 地址

因為在 dotnetcore 是沒有直接和硬件相關的,所以無法通過 WMI 的方法獲取當前設備的 Mac 地址

但是在 dotnet core 可以使用下面的代碼拿到本機所有的網卡地址,包括物理網卡和虛擬網卡

IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();   NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();   Console.WriteLine("Interface information for {0}.{1}  ",    computerProperties.HostName, computerProperties.DomainName);   if (nics == null || nics.Length < 1)   {    Console.WriteLine(" No network interfaces found.");    return;   }   Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length);   foreach (NetworkInterface adapter in nics)   {    Console.WriteLine();    Console.WriteLine(adapter.Name + "," + adapter.Description);    Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));    Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);    Console.Write(" Physical address ........................ : ");    PhysicalAddress address = adapter.GetPhysicalAddress();    byte[] bytes = address.GetAddressBytes();    for (int i = 0; i < bytes.Length; i++)    {     // Display the physical address in hexadecimal.     Console.Write("{0}", bytes[i].ToString("X2"));     // Insert a hyphen after each byte, unless we are at the end of the      // address.     if (i != bytes.Length - 1)     {      Console.Write("-");     }    }    Console.WriteLine();   }

運行代碼,下面是控制臺

Interface information for lindexi.github    Number of interfaces .................... : 6    Hyper-V Virtual Ethernet Adapter #4    ===================================    Interface type .......................... : Ethernet    Physical address ........................ : 00-15-5D-96-39-03    Hyper-V Virtual Ethernet Adapter #3    ===================================    Interface type .......................... : Ethernet    Physical address ........................ : 1C-1B-0D-3C-47-91    Software Loopback Interface 1    =============================    Interface type .......................... : Loopback    Physical address ........................ :    Microsoft Teredo Tunneling Adapter    ==================================    Interface type .......................... : Tunnel    Physical address ........................ : 00-00-00-00-00-00-00-E0    Hyper-V Virtual Ethernet Adapter    ================================    Interface type .......................... : Ethernet    Physical address ........................ : 5A-15-31-73-B0-9F    Hyper-V Virtual Ethernet Adapter #2    ===================================    Interface type .......................... : Ethernet    Physical address ........................ : 5A-15-31-08-13-B1

但是可以看到里面有很多不需要使用的網卡,從 堆棧 網找到的方法獲取當前有活躍的 ip 的網卡可以通過先判斷是不是本地巡回網絡等,然后判斷有沒有網絡

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊宁县| 都江堰市| 博罗县| 苍山县| 虞城县| 博湖县| 鹿邑县| 乐东| 金山区| 沂源县| 井冈山市| 会同县| 昌平区| 上虞市| 朝阳市| 泽普县| 会昌县| 怀柔区| 栖霞市| 丽江市| 万荣县| 永登县| 南和县| 蓬莱市| 莲花县| 阿拉尔市| 陈巴尔虎旗| 怀宁县| 富阳市| 浦东新区| 丹阳市| 蚌埠市| 余姚市| 平利县| 怀宁县| 个旧市| 来安县| 蓬莱市| 玛纳斯县| 丰镇市| 巢湖市|