此刻,我有一台虚拟服务器,其中有2个物理磁盘,磁盘1有2个分区,磁盘2也有2个分区。
WMI连接工作正常,我没有在此处添加代码...
ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_DiskDrive");
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(serviceScope, query1);
foreach (ManagementObject drives in searcher1.Get())
{
var deviceID = drives.Properties["DeviceID"].Value;
var partitionQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_DiskDriveToDiskPartition", drives.Path.RelativePath);
var partitionQuery = new ManagementObjectSearcher(partitionQueryText);
foreach (ManagementObject p in partitionQuery.Get())
{
var logicalDriveQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_LogicalDiskToPartition", p.Path.RelativePath);
var logicalDriveQuery = new ManagementObjectSearcher(logicalDriveQueryText);
foreach (ManagementObject ld in logicalDriveQuery.Get())
{
var physicalName = Convert.ToString(drives.Properties["Name"].Value);
var driveName = Convert.ToString(ld.Properties["Name"].Value);
var partDiskSize = Convert.ToUInt64(drives.Properties["Size"].Value)/ 1073741824;
var actualDiskSize = Convert.ToUInt64(ld.Properties["Size"].Value)/ 1073741824;
var percent = (partDiskSize * 100) / actualDiskSize;
}
}
}
输出看起来不错。但是看起来,关联者无法引用PhysicalDrive1(我不知道原因),还是我的代码错误?