すべての物理ディスク上のすべてのパーティション/ボリューム (非表示のシステム ボリュームも) を確認したい。ボリュームの情報には次のものが含まれている必要があります
- パーティション インデックス (例: "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"
}
多分誰でも助けることができます:-)