[C#] 纯文本查看 复制代码
public override List<Drive> GetDriveList()
{
List<Drive> list = new List<Drive>();
string queryString = this.UseAsteriskInWMI ? "SELECT * FROM Win32_DiskDrive" : "SELECT Caption, Description, DeviceID, FirmwareRevision, Index, Manufacturer, Model, Name, Partitions, SerialNumber, Size FROM Win32_DiskDrive";
List<Drive> result;
using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(this._managementScope, queryString, this._enumerationOptions))
{
foreach (ManagementBaseObject managementBaseObject in managementObjectSearcher.Get())
{
Drive drive = new Drive
{
Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Caption"]),
Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Description"]),
FirmwareRevision = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["FirmwareRevision"]),
Index = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject["Index"]),
Manufacturer = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Manufacturer"]),
Model = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Model"]),
Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Name"]),
Partitions = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject["Partitions"]),
SerialNumber = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["SerialNumber"]),
Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject["Size"])
};
string str = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='";
object obj = managementBaseObject["DeviceID"];
string queryString2 = str + ((obj != null) ? obj.ToString() : null) + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
using (ManagementObjectSearcher managementObjectSearcher2 = new ManagementObjectSearcher(this._managementScope, queryString2, this._enumerationOptions))
{
foreach (ManagementBaseObject managementBaseObject2 in managementObjectSearcher2.Get())
{
Partition partition = new Partition
{
Bootable = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["Bootable"]),
BootPartition = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["BootPartition"]),
Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Caption"]),
Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Description"]),
DiskIndex = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject2["DiskIndex"]),
Index = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject2["Index"]),
Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Name"]),
PrimaryPartition = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["PrimaryPartition"]),
Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject2["Size"]),
StartingOffset = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject2["StartingOffset"])
};
string str2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='";
object obj2 = managementBaseObject2["DeviceID"];
string queryString3 = str2 + ((obj2 != null) ? obj2.ToString() : null) + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
using (ManagementObjectSearcher managementObjectSearcher3 = new ManagementObjectSearcher(this._managementScope, queryString3, this._enumerationOptions))
{
foreach (ManagementBaseObject managementBaseObject3 in managementObjectSearcher3.Get())
{
Volume item = new Volume
{
Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Caption"]),
Compressed = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject3["Compressed"]),
Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Description"]),
FileSystem = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["FileSystem"]),
FreeSpace = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject3["FreeSpace"]),
Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Name"]),
Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject3["Size"]),
VolumeName = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["VolumeName"]),
VolumeSerialNumber = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["VolumeSerialNumber"])
};
partition.VolumeList.Add(item);
}
drive.PartitionList.Add(partition);
}
}
list.Add(drive);
}
}
result = list;
}
return result;
}