How to get Logical Disk for a specific Hard Disk using WMI?

2.9k views Asked by At

I am currently working on a project where I need to use WMI (Windows Management Interface). I need to find the Hard Disks, and Logical Disks and some information about them. For example I use the following code to get the physical disk:

hd = new HardDrive();
mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '"
                                        + disksModel[i] + "'");
foreach(ManagementObject moDisk in mosDisks.Get())
{
    string vari = moDisk["DeviceID"].ToString();
    hd.HardDiskModel = moDisk["Model"].ToString();
    hd.HardDiskType = moDisk["InterfaceType"].ToString();
    hd.HardDiskStatus = moDisk.Properties["Status"].Value.ToString();
    hd.HardDiskSize = moDisk.Properties["Size"].Value.ToString();
    hd.HardDiskName = moDisk.Properties["Name"].Value.ToString();
    hd.HardDiskSerialNo = moDisk.Properties["SerialNumber"].Value.ToString();
}

And then another query to get logical disks.

The only thing I'd like to know is: how can I make sure that I get the logical disks for a specific Hard Disk?

For example, if I have 3 Hard Disks(HD1, HD2, HD3), and 8 logical disks (c: to j:), how can I make sure in the code that for example c: and d: belong to HD1, e:, f:, g: and h: belong to HD2 and i: and j: belong to HD3?

1

There are 1 answers

0
RRUZ On BEST ANSWER

You can correlate the Win32_DiskDrive with the logical drives using the ASSOCIATORS OF sentence, these are the classes which you must use.

  • Win32_DiskDrive
  • Win32_DiskDriveToDiskPartition
  • Win32_DiskPartition
  • Win32_LogicalDiskToPartition
  • Win32_LogicalDisk

Some time ago I wrote a Delphi sample about this which you can check, Also you can found a C# sample on this SO question.