1 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
2 foreach (NetworkInterface adapter in nics)
3 {
4 //判断是否为以太网卡
5 //Wireless80211 无线网卡 Ppp 宽带连接
6 //Ethernet 以太网卡
7 //if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
8 //{
9 //获取以太网卡网络接口信息
10 IPInterfaceProperties ip = adapter.GetIPProperties();
11 string MAC = adapter.GetPhysicalAddress().ToString(); //获取单播地址集
12 UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
13 string fCardType = "未知网卡";
14 string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
15 RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
16 if (rk != null)
17 {
18 string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
19 string Characteristics = rk.GetValue("Characteristics", "").ToString();
20 int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
21 if (fPnpInstanceID.Length > 3 && (fPnpInstanceID.Substring(0, 3) == "PCI" || fPnpInstanceID.Substring(0, 3) == "USB"))
22 {
23 fCardType = "物理网卡";
24 }
25 else
26 {
27 fCardType = "虚拟网卡";
28 }
29
30 }
31 foreach (UnicastIPAddressInformation ipadd in ipCollection)
32 {
33 //InterNetwork IPV4地址 InterNetworkV6 IPV6地址
34 //Max MAX 位址
35 if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)//判断是否为ipv4
36 {
37 //Console.WriteLine(JsonConvert.SerializeObject(adapter));
38 Console.WriteLine($"描述(Description):{adapter.Description}");
39 Console.WriteLine($"名称(Name):{adapter.Name}");
40 Console.WriteLine($"网卡类型:{fCardType}");
41 Console.WriteLine($"网络操作类型(OperationalStatus):{adapter.OperationalStatus}");
42 Console.WriteLine($"网络接口类型(NetworkInterfaceType):{adapter.NetworkInterfaceType}");
43 Console.WriteLine($"MAC:{adapter.GetPhysicalAddress()}");
44 Console.WriteLine($"Address:{ipadd.Address}");
45 }
46 }
47 Console.WriteLine("\r\n");
48 }
49 Console.ReadKey();
50 }