国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

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

來源:懂視網 責編:小采 時間:2020-11-27 22:34:37
文檔

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

asp.net core 獲取 MacAddress 地址方法示例:本文告訴大家如何在 dotnet core 獲取 Mac 地址 因為在 dotnetcore 是沒有直接和硬件相關的,所以無法通過 WMI 的方法獲取當前設備的 Mac 地址 但是在 dotnet core 可以使用下面的代碼拿到本機所有的網卡地址,包括物理網卡和虛擬網卡 IPGlobalP
推薦度:
導讀asp.net core 獲取 MacAddress 地址方法示例:本文告訴大家如何在 dotnet core 獲取 Mac 地址 因為在 dotnetcore 是沒有直接和硬件相關的,所以無法通過 WMI 的方法獲取當前設備的 Mac 地址 但是在 dotnet core 可以使用下面的代碼拿到本機所有的網卡地址,包括物理網卡和虛擬網卡 IPGlobalP

本文告訴大家如何在 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 的網卡可以通過先判斷是不是本地巡回網絡等,然后判斷有沒有網絡

foreach (NetworkInterface adapter in nics.Where(c =>
 c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up))

獲取當前的網卡有沒 ip 有 ip 才是需要的

IPInterfaceProperties properties = adapter.GetIPProperties();

 var unicastAddresses = properties.UnicastAddresses;
 foreach (var temp in unicastAddresses.Where(temp =>
 temp.Address.AddressFamily == AddressFamily.InterNetwork))
 {
 // 這個才是需要的網卡
 }

簡單輸出網卡使用 adapter.GetPhysicalAddress().ToString() 輸出,如果需要輸出帶連接的請使用 GetAddressBytes 然后自己輸出

下面的代碼是我抽出來的,可以直接使用

public static void GetActiveMacAddress(string separator = "-")
 {
 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

 //Debug.WriteLine("Interface information for {0}.{1} ",
 // computerProperties.HostName, computerProperties.DomainName);
 if (nics == null || nics.Length < 1)
 {
 Debug.WriteLine(" No network interfaces found.");
 return;
 }

 var macAddress = new List<string>();

 //Debug.WriteLine(" Number of interfaces .................... : {0}", nics.Length);
 foreach (NetworkInterface adapter in nics.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(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
 //Debug.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.
 // Debug.Write($"{bytes[i]:X2}");
 // // Insert a hyphen after each byte, unless we are at the end of the 
 // // address.
 // if (i != bytes.Length - 1)
 // {
 // Debug.Write("-");
 // }
 //}

 //Debug.WriteLine("");

 //Debug.WriteLine(address.ToString());

 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
 {
 macAddress.Add(string.Join(separator, address.GetAddressBytes()));
 }
 }
 }
 }

上面的方法不僅是在 dotnet core 可以使用,在 dotnet framework 程序同樣調用,但是在 dotnet framework 還可以通過 WMI 獲取

在 dotnet framework 使用 WMI 獲取 MAC 地址方法

var managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
 var managementObjectCollection = managementClass.GetInstances();
 foreach (var managementObject in managementObjectCollection.OfType<ManagementObject>())
 {
 using (managementObject)
 {
 if ((bool) managementObject["IPEnabled"])
 {
 if (managementObject["MacAddress"] == null)
 {
 return string.Empty;
 }

 return managementObject["MacAddress"].ToString().ToUpper();
 }
 }
 }

輸出的格式是 5A:15:31:73:B0:9F 同時輸出是一個網卡

NetworkInterface.GetPhysicalAddress Method (System.Net.NetworkInformation)

PhysicalAddress Class (System.Net.NetworkInformation)

c# - .NET Core 2.x how to get the current active local network IPv4 address? - Stack Overflow

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

asp.net core 獲取 MacAddress 地址方法示例:本文告訴大家如何在 dotnet core 獲取 Mac 地址 因為在 dotnetcore 是沒有直接和硬件相關的,所以無法通過 WMI 的方法獲取當前設備的 Mac 地址 但是在 dotnet core 可以使用下面的代碼拿到本機所有的網卡地址,包括物理網卡和虛擬網卡 IPGlobalP
推薦度:
標簽: 地址 獲取 mac地址
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 中文国产成人精品少久久 | 99视频九九精品视频在线观看 | 国产一区二区三区日韩欧美 | 久久久久久久一精品 | 国产欧美日韩精品在线 | 亚洲欧美自拍一区 | 亚洲欧美日韩在线观看二区 | a一级毛片免费播放 | 国内一级野外a一级毛片 | 伊人伊成久久人综合网777 | 在线观看日韩欧美 | 久久久久女人精品毛片九一 | 亚洲va国产va欧美va综合 | 日韩欧美系列 | 一区二区三区视频在线观看 | 久久久久777777人人人视频 | 国产一区精品视频 | 亚洲欧美日韩在线 | 国内不卡1区2区 | 欧美区国产区 | 国产精品欧美韩国日本久久 | 亚洲欧洲在线视频 | 亚洲欧美另类日韩 | 日韩第一页在线 | 一级一级一级毛片 | 国产精品成人一区二区 | 中文字幕精品一区二区精品 | 99久久国产综合精品麻豆 | 亚洲天堂欧美 | 成人一级毛片 | 欧美中文日韩 | 欧美αv日韩αv另类综合 | 成人免费一级毛片在线播放视频 | 欧洲精品一区二区三区 | 亚洲尹人九九大色香蕉网站 | 国产在线观看精品一区二区三区91 | 国产成人精品一区二区 | 日韩电影一区二区 | 国产高清精品一区 | a毛片免费视频 | 亚洲一区日韩 |