List of all partitions on a disk

I am making a C # utility for a file system that is not supported by windows, which means that I cannot just access the disk. I need a way to list all partitions on the hard drive and write / format them.

+4
source share
2 answers

To list the partitions of a disk, you can use WMI.

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"]); } 
+7
source

You can use the following approach to get a Volume or DriveLetter on which a disk partition is mounted.

  • Win32_LogicalDiskToPartition
  • Win32_DiskDrive

From the Win32_DiskDrive class Win32_DiskDrive you can get DriveNumber by querying the Index property or retrieving the DriveNumber from Name attribute. Then run the Antecedent and Dependent request from Win32_LogicalDiskToPartition . For Antecedent you get the disk number and the partition in which it tries to display the volume, then extract the DriveLetter , for example, "C:", "D:", etc. From the Dependent property. Thus, using this logic, you can set LogicalDrives to a specific HardDisk . I use this logic in my component to get the LogicalDrive names ("C:", "D:", etc.) for a specific hard drive on my system.

0
source

All Articles