在这篇文章中,我们看到了如何枚举所有 WMI(Windows Management Intrumentation)命名空间和类。然后在这篇文章中,我们看到了一个查询系统以检索所有本地驱动器的示例:
ObjectQuery objectQuery = new ObjectQuery("SELECT Size, Name FROM Win32_LogicalDisk where DriveType=3");
我们所追求的属性如 Win32_LogicalDisk 的“大小”和“名称”。有一个简单的解决方案,因为我们可以选择对象查询中的所有属性。以下方法将打印 WMI 类中所有可用的属性、它们的类型和值:
private static void PrintPropertiesOfWmiClass(string namespaceName, string wmiClassName)
{
ManagementPath managementPath = new ManagementPath();
managementPath.Path = namespaceName;
ManagementScope managementScope = new ManagementScope(managementPath);
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM " + wmiClassName);
ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection objectCollection = objectSearcher.Get();
foreach (ManagementObject managementObject in objectCollection)
{
PropertyDataCollection props = managementObject.Properties;
foreach (PropertyData prop in props)
{
Console.WriteLine("Property name: {0}", prop.Name);
Console.WriteLine("Property type: {0}", prop.Type);
Console.WriteLine("Property value: {0}", prop.Value);
}
}
}
您需要以管理员身份使用 VS 运行它。此外,没有身份验证,因此我们将使用此代码来调查本地计算机上的类属性。否则,请参阅上面提到的帖子以获取从网络上的另一台计算机读取 WMI 对象的示例。
让我们看看在 cimv2/Win32_LocalTime 类中为我们准备了什么:
PrintPropertiesOfWmiClass("root\\cimv2", "Win32_LocalTime");
我得到以下输出:
我们再看一个:
PrintPropertiesOfWmiClass("root\\cimv2", "Win32_BIOS");
我的 PC 的 BIOS 属性中的一些有趣的属性值:
您可以在此处查看与诊断相关的所有帖子。