How can I find the drive containing a given section in C #?

Does anyone know how to programmatically determine which physical disk a given partition contains? Manually, I can find this information using Start-> Run-> diskmgmt.msc, where I see that (on my computer) the C: and D: partitions are on disk 1, E: and F: on disk 0.

This is to optimize some file crunching operations, doing them in parallel if the files are on different physical disks.

+1


source to share


4 answers


You can get this information using WMI from the System.Management namespace by querying the Win32_DiskDrive class .



The following is basic information about WMI in .NET.

+3


source


In addition to Arul's answer, here's a link that shows how to map drive association and lt; -> using WMI from VBS script: WMI Tasks: Disks and File Systems -> see the last example on the page.



Edit: Better yet, here's a nice WMI article using C # that just describes the exact relationship between the WMI classes you needed to get a disk (for example \\\\.\\PHYSICALDRIVE0

) that contains a logical drive (for example C:

)

+1


source


see HKEY_LOCAL_MACHINE \ SYSTEM \ MountedDevices

wmic path CIM_BasedOn get * > wmic-path-CIM_BasedOn-get.txt
wmic path CIM_DiskPartition get * > wmic-path-CIM_DiskPartition-get.txt
wmic path CIM_StorageExtent get * > wmic-path-CIM_StorageExtent-get.txt

      

see class: CIM_BasedOn - do not list hiden partition, CIM_DiskPartition -all, but do not see disk label, CIM_StorageExtent - GUID, shortcut, partition (hide too)

+1


source


Are you sure the partition is associated with only one drive? It can be striped / stretched / etc across multiple physical disks.

0


source







All Articles