LogicalDisk 与 DiskPartition 的 WMI 关联
WMI Association of LogicalDisk with DiskPartition
我正在尝试整理一个列表,其中显示我的计算机系统中的所有 LogicalDisk 实例以及它们关联的驱动器盘符。编码在 C# 中。
WMI classes Win32_LogicalDiskToPartition、Win32_DiskPartition 和 Win32_LogicalDisk 似乎是完成该工作的正确数据源:
Win32_LogicalDiskToPartition 包含 属性 "Antecedent" 显然链接到"DeviceId" 属性 of class Win32_DiskPartition
和 Win32_LogicalDiskToPartition 包含 属性 "Dependent" 显然链接到一个“DeviceId”属性 of class Win32_LogicalDisk
这是我的问题:
Antecedent属性Win32_LogicalDiskToPartitionreturns一个字符串值如:
\\HOME-PC\root\cimv2:Win32_DiskPartition.DeviceID=\"Disk #2, Partition #0\
但我只需要 Disk #2, Partition #0
将其与 DeviceId 属性 值 class Win32_DiskPartition.
依赖 属性 值的类似问题。
有没有办法获得这个子字符串(硬编码字符串解析除外)?
恐怕查询没有帮助,因为我还需要有关逻辑磁盘和关联磁盘分区的其他信息。我知道我必须覆盖具有多个驱动器号的扩展分区 - 这可以通过 StartingAddress 属性 的 Win32_LogicalDiskToPartition实例.
这种类型的枚举通常使用System.Management
ManagementObjectSearcher
来执行
这是一个顺序路径,您可以按照该路径检索系统中驱动器的信息:
Enumerate the Disk Drives => For Each [DeviceID] =>
Enumerate Disk Drive To Partition => For Each [DeviceID]
Enumerate Logical Disk To Partition
每个 class 中的对象都有其关联的属性:
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());
}
}
}
我正在尝试整理一个列表,其中显示我的计算机系统中的所有 LogicalDisk 实例以及它们关联的驱动器盘符。编码在 C# 中。
WMI classes Win32_LogicalDiskToPartition、Win32_DiskPartition 和 Win32_LogicalDisk 似乎是完成该工作的正确数据源:
Win32_LogicalDiskToPartition 包含 属性 "Antecedent" 显然链接到"DeviceId" 属性 of class Win32_DiskPartition
和 Win32_LogicalDiskToPartition 包含 属性 "Dependent" 显然链接到一个“DeviceId”属性 of class Win32_LogicalDisk
这是我的问题:
Antecedent属性Win32_LogicalDiskToPartitionreturns一个字符串值如:
\\HOME-PC\root\cimv2:Win32_DiskPartition.DeviceID=\"Disk #2, Partition #0\
但我只需要 Disk #2, Partition #0
将其与 DeviceId 属性 值 class Win32_DiskPartition.
依赖 属性 值的类似问题。
有没有办法获得这个子字符串(硬编码字符串解析除外)?
恐怕查询没有帮助,因为我还需要有关逻辑磁盘和关联磁盘分区的其他信息。我知道我必须覆盖具有多个驱动器号的扩展分区 - 这可以通过 StartingAddress 属性 的 Win32_LogicalDiskToPartition实例.
这种类型的枚举通常使用System.Management
ManagementObjectSearcher
来执行
这是一个顺序路径,您可以按照该路径检索系统中驱动器的信息:
Enumerate the Disk Drives => For Each [DeviceID] =>
Enumerate Disk Drive To Partition => For Each [DeviceID]
Enumerate Logical Disk To Partition
每个 class 中的对象都有其关联的属性:
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());
}
}
}