<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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久久亚洲综合精品| 成全视频高清免费观看电视剧 | 免费无码专区毛片高潮喷水| 久久免费观看国产99精品| 日韩成人在线免费视频| 亚洲国产无套无码av电影| 亚洲愉拍一区二区三区| 久久精品成人免费网站| 亚洲成aⅴ人片久青草影院| 亚洲国产成人va在线观看网址| 国产无限免费观看黄网站| 国外成人免费高清激情视频| 亚洲AV无码久久精品成人| 免费看黄福利app导航看一下黄色录像| 成人永久免费高清| 无码 免费 国产在线观看91| 在线精品免费视频| 精品一区二区三区免费毛片| 相泽亚洲一区中文字幕| 久久亚洲精品无码gv| 国国内清清草原免费视频99| 狠狠亚洲婷婷综合色香五月排名 | 九九99热免费最新版| 亚洲av综合avav中文| 免费下载成人电影| 香港经典a毛片免费观看看| 在线视频观看免费视频18| 亚洲福利在线视频| 精品国产免费一区二区三区香蕉 | 亚洲熟妇无码一区二区三区导航| 日韩一区二区免费视频| 中文字幕无线码免费人妻| 亚洲一区二区三区国产精品| 亚洲hairy多毛pics大全| 成年性午夜免费视频网站不卡| 亚洲H在线播放在线观看H| 日本XXX黄区免费看|