.net LogicalDisk与磁盘分区的关联

hrirmatl  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(132)

我正试图整理一个列表,其中显示了我的计算机系统中的所有LogicalDisk示例以及与它们关联的驱动器号。编码是用C#。
Windows类Win32_LogicalDisk ToPartitionWin32_LogicalDisk PartitionWin32_LogicalDisk似乎是完成该作业的正确数据源:

*Win32_LogicalNoteToPartition包含属性“Antecedent”,该属性显然链接到类Win32_NotePartition的“DeviceId”属性

  • 并且Win32_LogicalDisk ToPartition包含属性“Dependent”,该属性显然链接到类Win32_LogicalDisk的“DeviceId”属性

我的问题是

Win32_LogicalNoteToPartitionAntecedent 属性返回一个字符串值,如下所示:

\\\\HOME-PC\\root\\cimv2:Win32_DiskPartition.DeviceID=\"Disk #2, Partition #0\

但我只需要Disk #2, Partition #0就可以将它与类Win32_RectorPartitionDeviceId 属性值匹配。
与Dependent属性值类似的问题。
有没有一种方法可以获得这个子字符串(除了通过硬编码字符串解析)?
恐怕查询没有帮助,因为我还需要有关逻辑磁盘和相关磁盘分区的其他信息。我知道我必须覆盖具有多个驱动器号的扩展分区-这可以通过Win32_LogicalNoteToPartition示例的 StartingAddress 属性来完成。

n8ghc7c1

n8ghc7c11#

这种类型的枚举通常使用System.Management
ManagementObjectSearcher使用WQL ASSOCIATORS OF语句构建查询
这是一个顺序路径,您可以按照检索信息的驱动器在系统中:
枚举磁盘驱动器=>对于每个[设备ID] =>
枚举磁盘驱动器到分区=>对于每个[设备ID]
枚举逻辑磁盘到分区
每个类中的对象都有其关联的属性:
Disk Drives (MSDN)
Partition (MSDN)
Logical Disk (MSDN)

using System.Management;

 //Define an initial scope for the following queries
 var scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\CIMV2");

 //Select all Disk Drives
 var query = new SelectQuery("SELECT * FROM Win32_DiskDrive");
 //Options => Timeout infinite to avoid timeouts and forward only for speed
 var options = new EnumerationOptions();
 options.Timeout = EnumerationOptions.InfiniteTimeout;
 options.Rewindable = false;
 options.ReturnImmediately = true;

 //New root Management Object
 var searcher = new ManagementObjectSearcher(scope, query, options);

 //Enumerate all Disk Drives
 foreach (ManagementObject moDisk in searcher.Get())
 {
    //Query the associated partitions of the current DeviceID
    string assocQuery = "Associators of {Win32_DiskDrive.DeviceID='" + 
                        mobDisk.Properties["DeviceID"].Value.ToString() + "'}" +
                        "where AssocClass=Win32_DiskDriveToDiskPartition";
    var assocPart = new ManagementObjectSearcher(assocQuery);
    assocPart.Options.Timeout = EnumerationOptions.InfiniteTimeout;

    //For each Disk Drive, query the associated partitions
    foreach (ManagementObject moPart in assocPart.Get())
    {
       Console.WriteLine("DeviceID: {0}  BootPartition: {1}", 
                         moPart.Properties["DeviceID"].Value.ToString(), 
                         moPart.Properties["BootPartition"].Value.ToString());

       //Query the associated logical disk of the current PartitionID
       string logDiskQuery = "Associators of {Win32_DiskPartition.DeviceID='" + 
                              moPart.Properties["DeviceID"].Value.ToString() + "'} " +
                              "where AssocClass=Win32_LogicalDiskToPartition";

       var logDisk = new ManagementObjectSearcher(logDiskQuery);
       logDisk.Options.Timeout = EnumerationOptions.InfiniteTimeout;

       //For each partition, query the Logical Drives
       foreach (var logDiskEnu in logDisk.Get())
       {
          Console.WriteLine("Volume Name: {0}  Serial Number: {1}  System Name: {2}",
                            logDiskEnu.Properties["VolumeName"].Value.ToString(),
                            logDiskEnu.Properties["VolumeSerialNumber"].Value.ToString(),
                            logDiskEnu.Properties["SystemName"].Value.ToString());
          Console.WriteLine("Description: {0}  DriveType: {1}  MediaType: {2}",
                            logDiskEnu.Properties["Description"].Value.ToString(),
                            logDiskEnu.Properties["DriveType"].Value.ToString(),
                            logDiskEnu.Properties["MediaType"].Value.ToString());
       }
    }
 }

相关问题