逻辑磁盘与磁盘分区的 WMI 关联 [英] WMI Association of LogicalDisk with DiskPartition

查看:58
本文介绍了逻辑磁盘与磁盘分区的 WMI 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整理一个列表,其中显示了我的计算机系统中的所有 LogicalDisk 实例以及它们关联的驱动器号.编码使用 C#.

I am trying to put together a list which shows all LogicalDisk instances in my Computer System and the drive letters they have been associated with. Coding is in C#.

WMI 类 Win32_LogicalDiskToPartitionWin32_DiskPartitionWin32_LogicalDisk 似乎是完成这项工作的正确数据源:

The WMI classes Win32_LogicalDiskToPartition, Win32_DiskPartition and Win32_LogicalDisk appeared to be the right data sources to get that Job done:

  • Win32_LogicalDiskToPartition 包含属性Antecedent";显然链接到DeviceId";Win32_DiskPartition

  • Win32_LogicalDiskToPartition contains the property "Antecedent" which obviously links to a "DeviceId" property of class Win32_DiskPartition

Win32_LogicalDiskToPartition 包含属性Dependent";显然链接到DeviceId";Win32_LogicalDisk

and Win32_LogicalDiskToPartition contains the property "Dependent" which obviously links to a "DeviceId" property of class Win32_LogicalDisk

这是我的问题:

Win32_LogicalDiskToPartitionAntecedent 属性返回一个字符串值,如:

The Antecedent property of Win32_LogicalDiskToPartition returns a string value like:

\\\\HOME-PC\\root\\cimv2:Win32_DiskPartition.DeviceID=\"Disk #2, Partition #0\

但我只需要 Disk #2, Partition #0 将其与 Win32_DiskPartition 类的 DeviceId 属性值匹配.

but I need only Disk #2, Partition #0 to match it with the DeviceId property values of class Win32_DiskPartition.

Dependent 属性值的类似问题.

Similar Problem with the Dependent property value.

有没有办法获得这个子串(硬编码字符串解析除外)?

Is there a way to obtain this substring (except by hard coded string parsing)?

恐怕查询无济于事,因为我还需要有关逻辑磁盘和关联磁盘分区的其他信息.我知道我必须用多个驱动器号覆盖扩展分区 - 这可以通过 Win32_LogicalDiskToPartition 实例的 StartingAddress 属性来完成.

I am afraid a query does not help because I do also need additional information about the logical disk and the associated disk Partition. I am aware that I have to cover Extended partitions with multiple drive letters - this can be done with the StartingAddress property of the Win32_LogicalDiskToPartition instance.

推荐答案

这种类型的枚举通常使用 System.Management ManagementObjectSearcher
这是检索系统中驱动器信息时可以遵循的一种顺序路径:

This type of enumeration is usually performed using System.Management ManagementObjectSearcher
This is one sequential path you can follow to retrieve information on drives in a system:

枚举磁盘驱动器 =>对于每个 [DeviceID] =>
枚举磁盘驱动器到分区 =>对于每个 [DeviceID]
枚举逻辑磁盘到分区

Enumerate the Disk Drives => For Each [DeviceID] =>
Enumerate Disk Drive To Partition => For Each [DeviceID]
Enumerate Logical Disk To Partition

每个类中的对象都有其关联的属性:

Objects in each class have their associated properties:

磁盘驱动器 (MSDN)
分区 (MSDN)
逻辑磁盘 (MSDN)

using System.Management;

 //Define an initial scope for the following queries
 var scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\CIMV2");

 //Select all Disk Drives
 var query = new SelectQuery("SELECT * FROM Win32_DiskDrive");
 //Options => Timeout infinite to avoid timeouts and forward only for speed
 var options = new EnumerationOptions();
 options.Timeout = EnumerationOptions.InfiniteTimeout;
 options.Rewindable = false;
 options.ReturnImmediately = true;

 //New root Management Object
 var searcher = new ManagementObjectSearcher(scope, query, options);

 //Enumerate all Disk Drives
 foreach (ManagementObject moDisk in searcher.Get())
 {
    //Query the associated partitions of the current DeviceID
    string assocQuery = "Associators of {Win32_DiskDrive.DeviceID='" + 
                                         mobDisk.Properties["DeviceID"].Value.ToString() + "'}" +
                                         "where AssocClass=Win32_DiskDriveToDiskPartition";
    var assocPart = new ManagementObjectSearcher(assocQuery);
    assocPart.Options.Timeout = EnumerationOptions.InfiniteTimeout;

    //For each Disk Drive, query the associated partitions
    foreach (ManagementObject moPart in assocPart.Get())
    {
       Console.WriteLine("DeviceID: {0}  BootPartition: {1}", 
                         moPart.Properties["DeviceID"].Value.ToString(), 
                         moPart.Properties["BootPartition"].Value.ToString());

       //Query the associated logical disk of the current PartitionID
       string logDiskQuery = "Associators of {Win32_DiskPartition.DeviceID='" + 
                              moPart.Properties["DeviceID"].Value.ToString() + "'} " +
                              "where AssocClass=Win32_LogicalDiskToPartition";

       var logDisk = new ManagementObjectSearcher(logDiskQuery);
       logDisk.Options.Timeout = EnumerationOptions.InfiniteTimeout;

       //For each partition, query the Logical Drives
       foreach (var logDiskEnu in logDisk.Get())
       {
          Console.WriteLine("Volume Name: {0}  Serial Number: {1}  System Name: {2}",
                            logDiskEnu.Properties["VolumeName"].Value.ToString(),
                            logDiskEnu.Properties["VolumeSerialNumber"].Value.ToString(),
                            logDiskEnu.Properties["SystemName"].Value.ToString());
          Console.WriteLine("Description: {0}  DriveType: {1}  MediaType: {2}",
                            logDiskEnu.Properties["Description"].Value.ToString(),
                            logDiskEnu.Properties["DriveType"].Value.ToString(),
                            logDiskEnu.Properties["MediaType"].Value.ToString());
       }
    }
 }

这篇关于逻辑磁盘与磁盘分区的 WMI 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆