我怎样才能找到哪个硬盘在哪个分区上?

时间:2013-09-20 15:22:34

标签: c# winforms

对于exmaple我想删除分区磁盘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;
    }
}

但这不是我需要的。

1 个答案:

答案 0 :(得分:0)

我不确定这是你之后的事情,但这可以给出输出[驱动器号] - [分区] - [磁盘],如你的例子所示。这段代码我用来跟踪热交换磁盘,使用磁盘序列号。 “results”变量包含您在标签中需要的内容。

(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为空。