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


    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     /// <SUMMARY></SUMMARY>   
    11     /// 标题:区分本地网卡、虚拟网卡及无线网卡   
    12     /// 作者:X.X.Y   
    13     /// 日期:2009-08-03   
    14     /// 描述:测试环境 VS2008 + XP   
    15     /// <SUMMARY></SUMMARY>   
    16     class Program   
    17     {   
    18         static void Main(string[] args)   
    19         {   
    20             ShowNetworkInterfaceMessage();   
    21         }   
    22         /// <SUMMARY></SUMMARY>   
    23         /// 显示本机各网卡的详细信息   
    24         /// <SUMMARY></SUMMARY>   
    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 }

    原文地址:http://blog.csdn.net/sabty/archive/2009/08/03/4404488.aspx

  • 相关阅读:
    Win Form 项目中app.config读取和修改 [ZT]
    实现全站统一的Page_PreInit()等事件
    DateTime的所有格式化输出
    大三下的半学期快过去了。。
    SQL Server 中易混淆的数据类型
    解决RD2作业在IE和Fire Fox中CSS效果不同的问题~
    AJAX也广告?
    App_Code目录下的全局类
    怀疑我不是那种材料。。
    用了几天Asp.Net 2.0遇到的几个小问题
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1834427.html
Copyright © 2020-2023  润新知