Win32_LogicalDisk 能获取分区的卷标,剩余空间等信息。
现在如何将这2个结合到一起使用?
比如
1:1 C: system
1:2 d: tools
2:1 e: files
2:2 f: doc
其中 1:1 表示 第一个硬盘的第一个分区 同理 1:2 标识第一个硬盘的第二个分区
现在 Win32_DiskPartition Win32_DiskPartition 这2个类可以做到,但这2个类没有什么相关联的key
Win32_LogicalDisk中的Name为"c" Win32_DiskPartition中的Name为"Disk #0, Partition #1"
无法把2个结果关联到一起.
SelectQuery diskPartition = new SelectQuery("Win32_DiskPartition");
SelectQuery logicalDisk = new SelectQuery("Win32_LogicalDisk");
ManagementObjectSearcher serch1 = new ManagementObjectSearcher(diskPartition);
ManagementObjectSearcher serch2 = new ManagementObjectSearcher(logicalDisk);
foreach (ManagementObject disk in serch1.Get()) {
string id = disk["DiskIndex"].ToString() + disk["Index"].ToString() + disk["Size"].ToString();
foreach (ManagementObject p in serch2.Get())
{
if (disk["Name"] == p["Name"]) { //此处不知如何进行关联
Debug.WriteLine(p["Size"]);
}
}
}
2 个解决方案
#1
怎么没人来看下呢?
#2
query association:
Win32_DiskPartition assoc
VB代码:
以上来自:
http://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx
你自己翻译一下
Win32_DiskPartition assoc
VB代码:
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( _
"SELECT Caption, DeviceID FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
WScript.Echo "Disk drive Caption: " _
& wmiDiskDrive.Caption _
& VbNewLine & "DeviceID: " _
& " (" & wmiDiskDrive.DeviceID & ")"
'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
For Each wmiLogicalDisk In wmiLogicalDisks
WScript.Echo "Drive letter associated" _
& " with disk drive = " _
& wmiDiskDrive.Caption _
& wmiDiskDrive.DeviceID _
& VbNewLine & " Partition = " _
& wmiDiskPartition.DeviceID _
& VbNewLine & " is " _
& wmiLogicalDisk.DeviceID
Next
Next
Next
以上来自:
http://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx
你自己翻译一下
#1
怎么没人来看下呢?
#2
query association:
Win32_DiskPartition assoc
VB代码:
以上来自:
http://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx
你自己翻译一下
Win32_DiskPartition assoc
VB代码:
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( _
"SELECT Caption, DeviceID FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
WScript.Echo "Disk drive Caption: " _
& wmiDiskDrive.Caption _
& VbNewLine & "DeviceID: " _
& " (" & wmiDiskDrive.DeviceID & ")"
'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
For Each wmiLogicalDisk In wmiLogicalDisks
WScript.Echo "Drive letter associated" _
& " with disk drive = " _
& wmiDiskDrive.Caption _
& wmiDiskDrive.DeviceID _
& VbNewLine & " Partition = " _
& wmiDiskPartition.DeviceID _
& VbNewLine & " is " _
& wmiLogicalDisk.DeviceID
Next
Next
Next
以上来自:
http://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx
你自己翻译一下