I'm making a utility in C# for a filesystem that isn't supported by windows, which means that I can't just access the drive. I need a way to list all partitions on the hard disk and writing/formatting them.
-
2Not being able to access the drive doesn't leave you with much beyond make a photo of it. – Hans Passant Jul 04 '11 at 20:22
-
1@HansPassant I think he means drive as in 'C: drive' and 'D: drive', not as in 'hard disk drive'. – robertc Jul 04 '11 at 20:40
-
@CommunistPancake Please ask the question, improve and paste some code. – kenorb Feb 18 '15 at 23:35
2 Answers
To list disk partitions 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,511
- 1
- 34
- 59
-
3
-
I found this to be useful: https://gist.github.com/MiloszKrajewski/352dc8b8eb132d3a2bc7 You'll want to un-comment the commented-out write()'s for full info – dlchambers Jun 15 '18 at 18:09
You can use the following approach to get the Volume or DriveLetter on which the partition of disk is mounted.
- Win32_LogicalDiskToPartition
- Win32_DiskDrive
From the Win32_DiskDrive
class you can get the DriveNumber
by querying property Index
or extracting the DriveNumber
from Name
attribute. Then query Antecedent
and Dependent
from Win32_LogicalDiskToPartition
. In the Antecedent
value you will get the Disk Number and the partition it is trying to map the Volume, after that extract the DriveLetter
such as "C:", "D:" etc from the Dependent
property. So by using this logic you can get the LogicalDrives
mounted on particular HardDisk
.
I'm using this logic in my component to get the LogicalDrive
names ("C:", "D:" etc) for particular hard drive on my system.

- 2,838
- 3
- 32
- 47

- 169
- 1
- 2
- 9