使用WMI从物理驱动器C#通过字母获取驱动器

时间:2018-07-25 11:56:19

标签: c# wmi

我在这里有一个脚本,该脚本应该从连接到计算机的所有驱动器(也在远程系统上)检索磁盘信息。看来它正在运行,但据我所知,在切换到第二个硬盘后,出现了“找不到对象错误”。

此刻,我有一台虚拟服务器,其中有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(我不知道原因),还是我的代码错误?

0 个答案:

没有答案