我喜欢查看所有物理磁盘上的每个分区/卷(也是隐藏的系统卷)。卷的信息应包含
- 分区索引(例如“1”)
- 名称(例如“c:”),
- 标签(例如“Windows”)
- 容量(例如 200GB)
在我看来,“WMI”可能是解决此任务的正确选择。
示例输出可能与此类似:
- PHYSICALDRIVE4
- --> 0 - m: - Data - 2TB
- PHYSICALDRIVE1
- --> 0 - '' - System Reserved - 100MB
- --> 1 - c: - Windows - 100GB
- --> 2 - d: - Programs - 200GB
- PHYSICALDRIVE2
- --> 0 - '' - Hidden Recovery Partition - 50GB
- --> 1 - f: - data - 1TB
我在网上找到了几种解决方案来获取驱动器号 (c:) 与磁盘 ID (disk0) 的组合。 可以在此处找到其中一种解决方案。
public Dictionary<string, string> GetDrives()
{
var result = new Dictionary<string, string>();
foreach ( var drive in new ManagementObjectSearcher( "Select * from Win32_LogicalDiskToPartition" ).Get().Cast<ManagementObject>().ToList() )
{
var driveLetter = Regex.Match( (string)drive[ "Dependent" ], @"DeviceID=""(.*)""" ).Groups[ 1 ].Value;
var driveNumber = Regex.Match( (string)drive[ "Antecedent" ], @"Disk #(\d*)," ).Groups[ 1 ].Value;
result.Add( driveLetter, driveNumber );
}
return result;
}
此解决方案的问题在于它忽略了隐藏分区。输出字典将仅包含 4 个条目 (m,4 - c,1 - d,1 - f,2)。
这是因为使用“Win32_LogicalDiskToPartition”将“win32_logicalDisk”与“win32_diskpartion”组合在一起。但“win32_logicalDisk”不包含未分配的卷。
我只能在“win32_volume”中找到未分配的卷,但我无法将“win32_volume”与“win32_diskpartition”结合起来。
简化我的数据类应该是这样的:
public class Disk
{
public string Diskname; //"Disk0" or "0" or "PHYSICALDRIVE0"
public List<Partition> PartitionList;
}
public class Partition
{
public ushort Index //can be of type string too
public string Letter;
public string Label;
public uint Capacity;
//Example for Windows Partition
// Index = "1" or "Partition1"
// Letter = "c" or "c:"
// Label = "Windows"
// Capacity = "1000202039296"
//
//Example for System-reserved Partition
// Index = "0" or "Partition0"
// Letter = "" or ""
// Label = "System-reserved"
// Capacity = "104853504"
}
也许任何人都可以提供帮助:-)