Tell if a Drive is a partition or a separate HDD

2020-07-18 10:13发布

问题:

I am writing my own file search (why because I want to / can - not looking for an existing program). I can get all the drives in c# by using the DriveInfo.GetDrives() method. Ideally I would like to run the search in parallel only on drives that are separate disk and for partitions that are on the same drive run them sequential. This way I will not cause the drives on constantly seek as the GetDrives returns all partitions or removable media. I know I can tell the type if it is a USB Drives vs. a HDD? How can I accomplish this will the DriveInfo or any other methodology that?

回答1:

This related question shows how to find out using WMI (found in System.Management):

var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition");

foreach (var queryObj in searcher.Get())
{
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("Win32_DiskPartition instance");
    Console.WriteLine("Name:{0}", (string)queryObj["Name"]);
    Console.WriteLine("Index:{0}", (uint)queryObj["Index"]);
    Console.WriteLine("DiskIndex:{0}", (uint)queryObj["DiskIndex"]);
    Console.WriteLine("BootPartition:{0}", (bool)queryObj["BootPartition"]);
}