<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
        主站蜘蛛池模板: 亚洲第一黄片大全| 久久国产免费福利永久| 午夜影视在线免费观看| 亚洲一区二区影视| 18勿入网站免费永久| 亚洲自国产拍揄拍| 最近的免费中文字幕视频 | 性色午夜视频免费男人的天堂| 久久久久久久亚洲精品| www免费黄色网| 亚洲熟妇av一区二区三区漫画| 久久九九久精品国产免费直播| 国产亚洲美女精品久久久| 成在线人直播免费视频| 国产成人亚洲综合无码| 999zyz**站免费毛片| 久久久婷婷五月亚洲97号色| 希望影院高清免费观看视频| 亚洲国产视频久久| 国产乱子伦精品免费女| 一级中文字幕乱码免费| 亚洲av不卡一区二区三区| 黄页网站免费观看| 四虎成人精品国产永久免费无码 | 美女视频黄频a免费观看| 亚洲精品成人区在线观看| a色毛片免费视频| 亚洲成人一级电影| 免费黄色一级毛片| 香蕉免费在线视频| 亚洲成年人免费网站| 国产高清免费在线| 日韩精品无码免费专区午夜| 亚洲成av人片在线看片| 国产一级一片免费播放i| 成人性生交大片免费看中文| 亚洲人成黄网在线观看| 亚洲国产精品日韩专区AV| 免费黄色网址网站| 美女黄网站人色视频免费| 亚洲第一成年人网站|