首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 使用WMI获取网卡解决思路

2013-11-09 
c# 使用WMI获取网卡本帖最后由 mdjs198 于 2013-11-05 15:38:43 编辑c# 使用WMI获取网卡信息.第一次研究wm

c# 使用WMI获取网卡
本帖最后由 mdjs198 于 2013-11-05 15:38:43 编辑 c# 使用WMI获取网卡信息.
第一次研究wmi一堆东西看着迷糊.现在只做到

   ObjectQuery oq = new ObjectQuery("select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");

或者 
   ObjectQuery oq = new ObjectQuery("select * from Win32_NetworkAdapterConfiguration ");

拿到网卡信息。但这样并不符合需求。
前者只能拿到已启用的网卡(因为无线网卡可能是未启用状态,后期还要加上启动的功能),后者则拿到太多的网卡信息(显示了14个。。。。。)

想拿到如同控制面板-网络连接 (更改适配器) 中那样的数量。
我应该设置什么条件合适? 网卡 WMI c#
[解决办法]
Win32_NetworkAdapter
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
[解决办法]
public static void ShowNetworkInterfaceMessage()  
        {  
            NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();  
            foreach (NetworkInterface adapter in fNetworkInterfaces)  
            {  
                #region " 网卡类型 "  
                string fCardType = "未知网卡";  
                string fRegistryKey = "SYSTEM//CurrentControlSet//Control//Network//{4D36E972-E325-11CE-BFC1-08002BE10318}//" + adapter.Id + "//Connection";  
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);  
                if (rk != null)  
                {  
                    // 区分 PnpInstanceID   
                    // 如果前面有 PCI 就是本机的真实网卡  
                    // MediaSubType 为 01 则是常见网卡,02为无线网卡。  
                    string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();  
                    int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));  
                    if (fPnpInstanceID.Length > 3 &&  
                        fPnpInstanceID.Substring(0, 3) == "PCI")  
                        fCardType = "物理网卡";  
                    else if (fMediaSubType == 1)  
                        fCardType = "虚拟网卡";  
                    else if (fMediaSubType == 2)  
                        fCardType = "无线网卡";  
                }  
                #endregion  
                #region " 网卡信息 "  
                Console.WriteLine("-----------------------------------------------------------");


                Console.WriteLine("-- " + fCardType);
                Console.WriteLine("-----------------------------------------------------------");
                Console.WriteLine("Id .................. : {0}", adapter.Id); // 获取网络适配器的标识符  
                Console.WriteLine("Name ................ : {0}", adapter.Name); // 获取网络适配器的名称  
                Console.WriteLine("Description ......... : {0}", adapter.Description); // 获取接口的描述  
                Console.WriteLine("Interface type ...... : {0}", adapter.NetworkInterfaceType); // 获取接口类型  
                Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); // 获取 Boolean 值,该值指示网络接口是否设置为仅接收数据包。  
                Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast); // 获取 Boolean 值,该值指示是否启用网络接口以接收多路广播数据包。  
                Console.WriteLine("Speed ............... : {0}", adapter.Speed); // 网络接口的速度  
                Console.WriteLine("Physical Address .... : {0}", adapter.GetPhysicalAddress().ToString()); // MAC 地址  
                IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                        Console.WriteLine("Ip Address .......... : {0}", UnicastIPAddressInformation.Address); // Ip 地址  
                }
                Console.WriteLine();  

                #endregion  
            }


[解决办法]
好像微软官方提供有一个WMI查询代码生成器,你搜一下WMICodeCreator.zip就有了
不过如果追求性能的话,还是用.Net封装好的NetworkInterface类或者Win32 API吧
如果要统计网卡的性能数据,可以使用性能计数器
new PerformanceCounter("Network Interface", "Bytes Received/sec", name);
new PerformanceCounter("Network Interface", "Bytes Sent/sec", name);

热点排行