標籤:
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地址