开发者

List All Partitions On Disk [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_运维百科

Closed 8 years ago.

Improve this question

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.


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


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

  1. Win32_LogicalDiskToPartition
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜