ITPub博客

首页 > Linux操作系统 > Linux操作系统 > winform C# 获取区分物理网卡、虚拟网卡及无线网卡

winform C# 获取区分物理网卡、虚拟网卡及无线网卡

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-12-04 13:58:33 0 删除 编辑

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博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-621631/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766206