请参阅包含隐藏卷的磁盘管理信息

本文关键字:磁盘 管理 信息 包含 隐藏 请参阅 | 更新日期: 2023-09-27 18:08:34

我希望看到所有物理磁盘上的每个分区/卷(包括隐藏的系统卷)。卷的信息应该包含

  • 分区索引(例如:"1")
  • (如
  • 名称。"c:"),
  • (如
  • 标签。"窗口")
  • 容量(例如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:)与磁盘(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_logicaldisktoppartition"将"win32_logicalDisk"与"win32_diskpartition"组合在一起。但是"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"
}


也许有人可以帮忙:-)

请参阅包含隐藏卷的磁盘管理信息

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("''", "''''")))))

这将从分区获得LogicalDisks的集合(如果存在的话)。希望这类回答的问题,以防其他人有类似的问题。遗憾的是,MSFT_Partition仅适用于Windows 8/Server 2012及更高版本。