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) と組み合わせて取得するための解決策が Web でいくつか見つかりました。 これらの解決策の 1 つがここにあります

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

これにより、パーティションから LogicalDisks のコレクションが取得されます (存在する場合)。他の誰かが同様の問題を抱えていた場合に備えて、この種の質問に答えてくれることを願っています. 残念ながら、MSFT_Partition は Windows 8/Server 2012 以降でのみ使用できます。

于 2016-08-09T01:33:23.223 に答える