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; } } }