在这篇文章中,我们看到了如何使用 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("---------------------------------------");
}
}
}
}
这是我的电脑打印输出的摘录:
您可以在此处查看与诊断相关的所有帖子。