1

当前,我可以使用DriveInfo.GetDrives()在 c# 中获取所有驱动器及其标签。然后我可以通过这种方法获得分区的磁盘索引/索引。

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"]);
}

问题在于 DiskIndex、名称和索引基本上只是数字而不是卷标,即 C:\、D:\ 等...

那么底线我怎样才能将卷标(即 DriveInfo 上的名称属性)设置为 DiskIndex?使用这种方法或更好的方法都可以。

(这是以下内容:判断驱动器是分区还是单独的 HDD

编辑: 我确实找到了Win32_LogicalDisk的管理查询,然后是Win32_LogicalDiskToPartition。LogicalDisk 具有卷,LogicalDisktoParition 提供映射。但是,我似乎无法弄清楚如何获取地图。我尝试寻找 JOIN 并选择值,但找不到任何关于如何在没有 c# 代码中大量循环的情况下进行连接的信息。

4

2 回答 2

6

前段时间我有同样的问题,我用这些代码来做:

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;

和两个子方法:

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;

        }

我希望这个解决方案有用。对我来说,这个结果如下图 硬盘信息

于 2014-02-28T12:24:24.050 回答
3

您需要使用Win32_LogicalDisk类。

编辑:你是正确的Win32_LogicalDiskToPartition。是Win32_LogicalDiskWin32_DiskPartition之间的链接。在Win32_LogicalDiskToPartition类上,这两个属性显示链接,

PS> Get-WmiObject -Class Win32_LogicalDiskToPartition

前件:\\computer\root\cimv2:Win32_DiskPartition.DeviceID="磁盘#0,分区#1"

依赖:\\computer\root\cimv2: Win32_LogicalDisk.DeviceID="D:"

只需解析这两个属性并适当地过滤其他类。

于 2012-02-03T15:15:59.620 回答