02 | using System.Collections.Generic; |
05 | using System.Net.NetworkInformation; |
06 | using System.Net.Sockets; |
07 | using Microsoft.Win32; |
10 | /// <summary></summary> |
11 | /// 标题:区分本地网卡、虚拟网卡及无线网卡 |
14 | /// 描述:测试环境 VS2008 + XP |
15 | /// <summary></summary> |
18 | static void Main( string [] args) |
20 | ShowNetworkInterfaceMessage(); |
22 | /// <summary></summary> |
24 | /// <summary></summary> |
25 | public static void ShowNetworkInterfaceMessage() |
27 | NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); |
28 | foreach (NetworkInterface adapter in fNetworkInterfaces) |
31 | string fCardType = "未知网卡" ; |
32 | string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection" ; |
33 | RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false ); |
39 | string fPnpInstanceID = rk.GetValue( "PnpInstanceID" , "" ).ToString(); |
40 | int fMediaSubType = Convert.ToInt32(rk.GetValue( "MediaSubType" , 0)); |
41 | if (fPnpInstanceID.Length > 3 && |
42 | fPnpInstanceID.Substring(0, 3) == "PCI" ) |
44 | else if (fMediaSubType == 1) |
46 | else if (fMediaSubType == 2) |
51 | Console.WriteLine( "-----------------------------------------------------------" ); |
52 | Console.WriteLine( "-- " + fCardType); |
53 | Console.WriteLine( "-----------------------------------------------------------" ); |
54 | Console.WriteLine( "Id .................. : {0}" , adapter.Id); |
55 | Console.WriteLine( "Name ................ : {0}" , adapter.Name); |
56 | Console.WriteLine( "Description ......... : {0}" , adapter.Description); |
57 | Console.WriteLine( "Interface type ...... : {0}" , adapter.NetworkInterfaceType); |
58 | Console.WriteLine( "Is receive only...... : {0}" , adapter.IsReceiveOnly); |
59 | Console.WriteLine( "Multicast............ : {0}" , adapter.SupportsMulticast); |
60 | Console.WriteLine( "Speed ............... : {0}" , adapter.Speed); |
61 | Console.WriteLine( "Physical Address .... : {0}" , adapter.GetPhysicalAddress().ToString()); |
62 | IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties(); |
63 | UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses; |
64 | foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) |
66 | if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) |
67 | Console.WriteLine( "Ip Address .......... : {0}" , UnicastIPAddressInformation.Address); |
原文地址:http://blog.youkuaiyun.com/sabty/archive/2009/08/03/4404488.aspx