简体   繁体   中英

How can i find which hard disk is on which partition?

For exmaple i want to delete the partition disk C: is on create new partition on this C: and format it and install new window.

How do i know if C: is on partition 0 or 1 or 2 ? I want to get list of all hard disks i have and show each one on what partition for example:

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

I tried this:

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;
    }
}

But it's not what i needed.

I'm not sure that this is what your'e after but this can give the output [drive letter] - [partition] - [Disk] as shown in your example. This code something I'm using to keep track of hotswap disks, using the disk serial number. The "results" variable contains the stuff you want in your labels.

(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;
    }
}

Observe that LogicalDrive.Partition will be null for network drives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM