0

我喜欢查看所有物理磁盘上的每个分区/卷(也是隐藏的系统卷)。卷的信息应包含

  • 分区索引(例如“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"
}


也许任何人都可以提供帮助:-)

4

1 回答 1

0

root\microsoft\windows\storage 中的 MSFT_Partition 包含所有分区,包括隐藏分区。我从该类中获取 DiskNumber 和 Offset,然后将它们与 Win32_DiskPartition 中的 DiskIndex 和 StartingOffset 值进行匹配。该组合应提供唯一标识符。

从 Win32_DiskDrive 开始,然后使用上面的方法获取分区。MSFT_Partition 对象还有一个名为 AccessPaths 的属性,它将包含 Win32_Volume 中的 DeviceID,您可以检查它以将卷与分区相关联。Win32_LogicalDisk 然后也可以使用您描述的方法检查分区,例如:

using (ManagementObjectSearcher LogicalDiskSearcher = new ManagementObjectSearcher(new ManagementScope(ManagementPath.DefaultPath), new ObjectQuery(String.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID=\"{0}\"}} WHERE AssocClass = Win32_LogicalDiskToPartition", ((string)Partition["DeviceID"]).Replace("\\", "\\\\")))))

这将从分区中获取逻辑磁盘的集合(如果存在)。希望这种回答问题,以防其他人遇到类似问题。不幸的是,MSFT_Partition 仅适用于 Windows 8/Server 2012 及更高版本。

于 2016-08-09T01:33:23.223 回答