问题
Current I am able to get all the Drives and their labels in c# by using the DriveInfo.GetDrives(). Then I am able to get the Disk Index / Index of the Partition by this methodology.
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition");
foreach (var queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DiskPartition instance");
Console.WriteLine("Name:{0}", (string)queryObj["Name"]);
Console.WriteLine("Index:{0}", (uint)queryObj["Index"]);
Console.WriteLine("DiskIndex:{0}", (uint)queryObj["DiskIndex"]);
Console.WriteLine("BootPartition:{0}", (bool)queryObj["BootPartition"]);
}
The problem with this is that the DiskIndex, Name, and Index are basically just numbers and not the Volume Label i.e. C:\, D:\, etc...
So bottom line how can I make the Volume Label which is the Name Proprty on the DriveInfo to the DiskIndex? Either using this methodology or a better way will work.
(This is a follow to: Tell if a Drive is a partition or a separate HDD)
EDIT: I did find for the Management Query of the Win32_LogicalDisk and then the Win32_LogicalDiskToPartition. the LogicalDisk has the volume and the LogicalDisktoParition provides the mapping. However, I cannot seem to figure out how to get the map. I tried looking for a JOIN and selecting the values but couldn't find anything on how do this join without extensive looping in the c# code.
回答1:
You need to use the Win32_LogicalDisk class.
Edit: You are correct Win32_LogicalDiskToPartition. Is the link between Win32_LogicalDisk and Win32_DiskPartition. On Win32_LogicalDiskToPartition class, these two properties show the links,
PS> Get-WmiObject -Class Win32_LogicalDiskToPartition
Antecedent : \\computer\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"
Dependent : \\computer\root\cimv2:Win32_LogicalDisk.DeviceID="D:"
Just parse these two properties and filter the the other classes appropriately.
回答2:
some time ago i had same problem and i do it with these code :
ListViewGroup hddgrp;
lstHDD.Columns.Add("Disk");
lstHDD.Columns.Add("Patition");
lstHDD.Columns.Add("Free Space");
lstHDD.Columns.Add("Total Space");
lstHDD.View = View.Details;
String DiskName = "";
String PartState = "";
String PartName = "";
String PartFree = "";
ManagementObjectSearcher hdd = new ManagementObjectSearcher("Select * from Win32_DiskDrive");
foreach (ManagementObject objhdd in hdd.Get())
{
PartState = "";
DiskName = "Disk " + objhdd["Index"].ToString() + " : " + objhdd["Caption"].ToString().Replace(" ATA Device", "") +
" (" + Math.Round( Convert.ToDouble(objhdd["Size"]) / 1073741824,1) + " GB)";
hddgrp = lstHDD.Groups.Add(DiskName, DiskName);
ObjCount = Convert.ToInt16(objhdd["Partitions"]);
ManagementObjectSearcher partitions = new ManagementObjectSearcher(
"Select * From Win32_DiskPartition Where DiskIndex='" + objhdd["Index"].ToString() + "'");
foreach(ManagementObject part in partitions.Get())
{
PartName = part["DeviceID"].ToString();
if (part["Bootable"].ToString() == "True" && part["BootPartition"].ToString() == "True")
PartState = "Recovery";
else
{
ManagementObjectSearcher getdisks = new ManagementObjectSearcher
("Select * From Win32_LogicalDiskToPartition Where ");
PartState = GetPartName(PartName);
PartFree = GetFreeSpace(PartState);
PartState = "Local Disk (" + PartState + ")";
}
lstHDD.Items.Add(new ListViewItem(new String[] { "Partition " + part["Index"].ToString(),
PartState,PartFree ,Math.Round( Convert.ToDouble(part["Size"].ToString()) / 1073741824,1) + " GB"}, hddgrp));
}
}
lstHDD.Columns[0].Width = 80;
lstHDD.Columns[1].Width = 120;
lstHDD.Columns[2].Width = 100;
lstHDD.Columns[3].Width = 100;
and two sub method :
private String GetFreeSpace(String inp)
{
String totalspace = "", freespace = "", freepercent = "";
Double sFree = 0, sTotal = 0, sEq = 0;
ManagementObjectSearcher getspace = new ManagementObjectSearcher("Select * from Win32_LogicalDisk Where DeviceID='" + inp +"'");
foreach (ManagementObject drive in getspace.Get())
{
if (drive["DeviceID"].ToString() == inp)
{
freespace = drive["FreeSpace"].ToString();
totalspace = drive["Size"].ToString();
sFree = Convert.ToDouble(freespace);
sTotal = Convert.ToDouble(totalspace);
sEq = sFree * 100 / sTotal;
freepercent = (Math.Round((sTotal - sFree) / 1073741824, 2)).ToString() + " (" + Math.Round(sEq,0).ToString() + " %)";
return freepercent;
}
}
return "";
}
private String GetPartName(String inp)
{
//MessageBox.Show(inp);
String Dependent = "", ret = "";
ManagementObjectSearcher LogicalDisk = new ManagementObjectSearcher("Select * from Win32_LogicalDiskToPartition");
foreach (ManagementObject drive in LogicalDisk.Get())
{
if (drive["Antecedent"].ToString().Contains(inp))
{
Dependent = drive["Dependent"].ToString();
ret = Dependent.Substring(Dependent.Length - 3, 2);
break;
}
}
return ret;
}
I hope this solution be useful. for me this result is as below picture
来源:https://stackoverflow.com/questions/9130658/map-a-diskindex-to-a-volume-label