在 C# .NET 中使用 WMI 查找所有网络适配器

这篇文章中,我们看到了如何使用 Windows Management Intrumentation (WMI) 检索所有逻辑驱动器。我们将采用非常相似的技术来枚举所有网络适配器。

以下代码打印在本地 - “root” - 计算机上找到的所有网络驱动器的所有非空属性:

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;
using static System.Threading.Thread;
using System.Management;

namespace WMI

{
    class Program
    {
        static void Main(string[] args)
        {

            ManagementObjectSearcher networkAdapterSearcher = new ManagementObjectSearcher("root\\cimv2", "select * from Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objectCollection = networkAdapterSearcher.Get();

            Console.WriteLine("There are {0} network adapaters: ", objectCollection.Count);

            foreach (ManagementObject networkAdapter in objectCollection)
            {
                PropertyDataCollection networkAdapterProperties = networkAdapter.Properties;
                foreach (PropertyData networkAdapterProperty in networkAdapterProperties)
                {
                    if (networkAdapterProperty.Value != null)
                    {
                        Console.WriteLine("Network adapter property name: {0}", networkAdapterProperty.Name);
                        Console.WriteLine("Network adapter property value: {0}", networkAdapterProperty.Value);
                    }
                }
                Console.WriteLine("---------------------------------------");

            }
        }
    }
}


这是我的电脑打印输出的摘录:

您可以在此处查看与诊断相关的所有帖子。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值