0

例如,我想删除分区磁盘 C: 在此 C: 上创建新分区并格式化并安装新窗口。

我如何知道 C: 是否在分区 0 或 1 或 2 上?我想获取我拥有的所有硬盘的列表,并在每个分区上显示每个硬盘,例如:

label1.Text will show: C: Partition 0
label2.Text will show: D: Partition 1

我试过这个:

string system_disk = Path.GetPathRoot(Environment.SystemDirectory).TrimEnd('\\');
using (var m1 = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" + system_disk + "'} WHERE ResultClass=Win32_DiskPartition"))
{
    foreach (var i1 in m1.Get())
    {
        using (var m2 = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + i1["DeviceID"] + "'} WHERE ResultClass=Win32_DiskDrive"))
        {
            foreach (var i2 in m2.Get())
            {
                label1.Text = string.Format("Type: " + i2["MediaType"]);
                label2.Text = string.Format("Model: " + i2["Name"]);
                break;
            }
        }
        break;
    }
}

但这不是我需要的。

4

1 回答 1

0

我不确定这是你所追求的,但这可以给出输出 [驱动器号] - [分区] - [磁盘],如您的示例所示。这段代码是我用来跟踪热插拔磁盘的,使用磁盘序列号。“结果”变量包含您在标签中想要的内容。

(using System.IO, System.Management, System.Linq, System.Collections.Generic...)

class HardDrive
{
    public string DiskIndex { get; set; }
    public string Model { get; set; }
    public string Type { get; set; }
    public string SerialNo { get; set; }
}

class Partition
{
    public string PartitionIndex { get; set; }
    public HardDrive HardDrive { get; set; }
}

class LogicalDrive
{
    public string Name { get; set; }
    public Partition Partition { get; set; }
}

// ... (class/method declaration etc)
var results = new List<LogicalDrive>();
var logicalDrives = DriveInfo.GetDrives().Where(x => x.IsReady);
var partitions = new Dictionary<string, Partition>();
var hardDrives = new Dictionary<string, HardDrive>();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach (ManagementObject mo in searcher.Get())
{
    var hd = new HardDrive();
    hd.Model = mo["Model"].ToString();
    hd.Type = mo["InterfaceType"].ToString();
    hd.SerialNo = mo["SerialNumber"].ToString().Trim();
    hd.DiskIndex = mo["Index"].ToString().Trim();
    hardDrives.Add(hd.DiskIndex, hd);
}

foreach (var logicalDrive in logicalDrives)
{
    var res = new LogicalDrive { Name = logicalDrive.Name };
    results.Add(res);
    var driveName = logicalDrive.Name.Trim('\\');
    var queryString = String.Format("ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{0}'}} WHERE ResultClass=Win32_DiskPartition", 
                driveName);
    searcher = new ManagementObjectSearcher(queryString);
    foreach (ManagementObject mo in searcher.Get())
    {
        var partitionIndex = mo["Index"].ToString().Trim();
        var diskIndex = mo["DiskIndex"].ToString().Trim();
        var key = partitionIndex + "-" + diskIndex;
        Partition p;
        if (!partitions.TryGetValue(key, out p))
        {
            p = new Partition
            {
                PartitionIndex = partitionIndex,
                HardDrive = hardDrives[diskIndex]
            };
            partitions.Add(key, p);
        }
        res.Partition = p;
    }
}

请注意,LogicalDrive.Partition对于网络驱动器,它将为空。

于 2013-09-26T12:34:16.580 回答