First, in the example you found, I would never rely on the entries in the WMI queries to be always the same order. The WMI queries can have an order by clauses. With that said, that example give you an excellent starting point. What you need to do is get the index of the disk drive for the boot partition. To do that, we need to query
Win32_DiskPartition
class. In this class, you are looking for the drive index where the
BootPartition
property is true. The sample code below should help you out.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject managementObject in searcher.Get())
{
Console.WriteLine(managementObject["DeviceId"].ToString());
Console.WriteLine(managementObject["Index"].ToString());
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskPartition");
foreach (ManagementObject managementObject in searcher.Get())
{
Console.WriteLine(managementObject["DeviceId"].ToString());
Console.WriteLine(managementObject["Index"].ToString());
Console.WriteLine(managementObject["Bootable"].ToString());
Console.WriteLine(managementObject["BootPartition"].ToString());
}
Console.ReadKey();