C#讀取物理網卡資訊及其對應的IP地址

來源:互聯網
上載者:User

標籤:

using Microsoft.Win32;using System;using System.Collections;using System.Collections.Generic;using System.Net.NetworkInformation;using System.Net.Sockets;namespace Common{    public class NetworkHelper    {        /// <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 網卡資訊                System.Console.WriteLine("-----------------------------------------------------------");                System.Console.WriteLine("-- " + fCardType);                System.Console.WriteLine("-----------------------------------------------------------");                System.Console.WriteLine("Id .................. : {0}", adapter.Id); // 擷取網路介面卡的標識符                  System.Console.WriteLine("Name ................ : {0}", adapter.Name); // 擷取網路介面卡的名稱                  System.Console.WriteLine("Description ......... : {0}", adapter.Description); // 擷取介面的描述                  System.Console.WriteLine("Interface type ...... : {0}", adapter.NetworkInterfaceType); // 擷取介面類型                  System.Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); // 擷取 Boolean 值,該值指示網路介面是否設定為僅接收資料包。                  System.Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast); // 擷取 Boolean 值,該值指示是否啟用網路介面以接收多路廣播資料包。                  System.Console.WriteLine("Speed ............... : {0}", adapter.Speed); // 網路介面的速度                  System.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)                        System.Console.WriteLine("Ip Address .......... : {0}", UnicastIPAddressInformation.Address); // Ip 地址                  }                System.Console.WriteLine();                #endregion            }            System.Console.ReadKey();        }        /// <summary>        /// 獲得本機真實物理網卡IP        /// </summary>        /// <returns></returns>        public static IList<string> GetPhysicsNetworkCardIP()        {            var networkCardIPs = new List<string>();            NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();            foreach (NetworkInterface adapter in fNetworkInterfaces)            {                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 就是原生真實網卡                      string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();                    int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));                    if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")                    {                        IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();                        UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;                        foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)                        {                            if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)                            {                                networkCardIPs.Add(UnicastIPAddressInformation.Address.ToString()); //Ip 地址                            }                        }                                            }                }            }            return networkCardIPs;        }    }}

  

C#讀取物理網卡資訊及其對應的IP地址

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.