winform C# 獲取區分物理網路卡、虛擬網路卡及無線網路卡

iDotNetSpace發表於2009-12-04

01 using System;    

02 using System.Collections.Generic;    

03 using System.Text;    

04 using System.Net;    

05 using System.Net.NetworkInformation;    

06 using System.Net.Sockets;    

07 using Microsoft.Win32;    

08 namespace ConsoleDemo    

09 {    

10     ///

    

11     /// 標題:區分本地網路卡、虛擬網路卡及無線網路卡    

12     /// 作者:X.X.Y    

13     /// 日期:2009-08-03    

14     /// 描述:測試環境 VS2008 + XP    

15     ///

    

16     class Program    

17     {    

18         static void Main(string[] args)    

19         {    

20             ShowNetworkInterfaceMessage();    

21         }    

22         ///

    

23         /// 顯示本機各網路卡的詳細資訊    

24         ///

    

25         public static void ShowNetworkInterfaceMessage()    

26         {    

27             NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();    

28             foreach (NetworkInterface adapter in fNetworkInterfaces)    

29             {   

30                 #region " 網路卡型別 "    

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

34                 if (rk != null)    

35                 {    

36                     // 區分 PnpInstanceID     

37                     // 如果前面有 PCI 就是本機的真實網路卡    

38                     // MediaSubType 為 01 則是常見網路卡,02為無線網路卡。    

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")    

43                         fCardType = "物理網路卡";    

44                     else if (fMediaSubType == 1)    

45                         fCardType = "虛擬網路卡";    

46                     else if (fMediaSubType == 2)    

47                         fCardType = "無線網路卡";    

48                 }   

49                 #endregion   

50                 #region " 網路卡資訊 "    

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); // 獲取 Boolean 值,該值指示網路介面是否設定為僅接收資料包。    

59                 Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast); // 獲取 Boolean 值,該值指示是否啟用網路介面以接收多路廣播資料包。    

60                 Console.WriteLine("Speed ............... : {0}", adapter.Speed); // 網路介面的速度    

61                 Console.WriteLine("Physical Address .... : {0}", adapter.GetPhysicalAddress().ToString()); // MAC 地址    

62                 IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();    

63                 UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;    

64                 foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)    

65                 {    

66                     if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)    

67                         Console.WriteLine("Ip Address .......... : {0}", UnicastIPAddressInformation.Address); // Ip 地址    

68                 }    

69                 Console.WriteLine();   

70                 #endregion    

71             }    

72             Console.ReadKey();    

73         }    

74     }    

75 }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-621631/,如需轉載,請註明出處,否則將追究法律責任。

相關文章