【发布时间】:2016-12-23 19:54:28
【问题描述】:
如何在 c# 中获取我的计算机上的硬盘及其分区(它们的逻辑驱动器)列表? 我正在寻找能给我类似结果的代码
harddisk0: 分区是 C,D
harddisk1: 分区是 C,F,D
我试过这个代码
foreach (ManagementObject drive in search.Get())
{
string antecedent = drive["DeviceID"].ToString();
// the disk we're trying to find out about
antecedent = antecedent.Replace(@"\", "\\");
// this is just to escape the slashes
string query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='"
+ antecedent
+ "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
using (ManagementObjectSearcher partitionSearch = new ManagementObjectSearcher(query))
{
foreach (ManagementObject part in partitionSearch.Get())
{
//...pull out the partition information
Console.WriteLine("Dependent : {0}", part["Dependent"]);
}
}
}
知道从属是对表示驻留在磁盘驱动器上的磁盘分区的实例的引用。 但是我得到了 Not Found 的异常
请问我应该写什么?
【问题讨论】:
-
不,我的问题没有通过这个链接解决stackoverflow.com/questions/6575727/list-all-partitions-on-disk
标签: c#