C#如何獲取物理網卡,虛擬網卡,以及無線網卡


就不廢話了,直接上代碼

/// <summary></summary>  
        /// 顯示本機各網卡的詳細信息  
        /// <summary></summary>  
        public static void ShowNetworkInterfaceMessage()
        {
            NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in fNetworkInterfaces)
            {
                #region " 網卡類型 "
                string fCardType = "未知網卡";
                string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                if (rk != null)
                {
                    // 區分 PnpInstanceID   
                    // 如果前面有 PCI 就是本機的真實網卡  
                    // MediaSubType 為 01 則是常見網卡,02為無線網卡。  
                    string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                    int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                    if (fPnpInstanceID.Length > 3 &&
                        fPnpInstanceID.Substring(0, 3) == "PCI")
                        fCardType = "物理網卡";
                    else if (fMediaSubType == 1)
                        fCardType = "虛擬網卡";
                    else if (fMediaSubType == 2)
                        fCardType = "無線網卡";
                }
                #endregion
                #region 網卡信息
                Console.WriteLine("-----------------------------------------------------------");
                Console.WriteLine("-- " + fCardType);
                Console.WriteLine("-----------------------------------------------------------");
                Console.WriteLine("Id .................. : {0}", adapter.Id); // 獲取網絡適配器的標識符  
                Console.WriteLine("Name ................ : {0}", adapter.Name); // 獲取網絡適配器的名稱  
                Console.WriteLine("Description ......... : {0}", adapter.Description); // 獲取接口的描述  
                Console.WriteLine("Interface type ...... : {0}", adapter.NetworkInterfaceType); // 獲取接口類型  
                Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); // 獲取 Boolean 值,該值指示網絡接口是否設置為僅接收數據包。  
                Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast); // 獲取 Boolean 值,該值指示是否啟用網絡接口以接收多路廣播數據包。  
                Console.WriteLine("Speed ............... : {0}", adapter.Speed); // 網絡接口的速度  
                Console.WriteLine("Physical Address .... : {0}", adapter.GetPhysicalAddress().ToString()); // MAC 地址  
                
                IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                        Console.WriteLine("Ip Address .......... : {0}", UnicastIPAddressInformation.Address); // Ip 地址  
                }
                Console.WriteLine();
                #endregion
            }
            Console.ReadKey();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM