Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get drive model from drive letter

Tags:

c#

I want to get the model name from a drive letter. For example Crucial_CT256MX100SSD1 is the model of my drive C:\

The model can be retrieved with a simple WMI query,

var hdd = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index = '0'")
    .Get()
    .Cast<ManagementObject>()
    .First();
MessageBox.Show(hdd["Model"].ToString());

However, I cannot filter the query with the drive letter.

Unfortunately Win32_LogicalDisk doesn't have the model of the drive.

I don't have more ideas.

like image 763
fakemeta Avatar asked Dec 23 '14 22:12

fakemeta


Video Answer


3 Answers

I wrote you a function that should do what you need:

class Program
{
    static void Main(string[] args)
    {
        const string drive = "C:";

        Console.WriteLine("Drive {0}'s Model Number is {1}", drive, GetModelFromDrive(drive));
    }

    public static string GetModelFromDrive(string driveLetter)
    {
        // Must be 2 characters long.
        // Function expects "C:" or "D:" etc...
        if (driveLetter.Length != 2)
            return "";

        try
        {
            using (var partitions = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" + driveLetter +
                                             "'} WHERE ResultClass=Win32_DiskPartition"))
            {
                foreach (var partition in partitions.Get())
                {
                    using ( var drives = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" +
                                                         partition["DeviceID"] +
                                                         "'} WHERE ResultClass=Win32_DiskDrive"))
                    {
                        foreach (var drive in drives.Get())
                        {
                            return (string) drive["Model"];
                        }
                    }
                }
            }
        }
        catch
        {
            return "<unknown>";
        }

        // Not Found
        return "<unknown>";
    }
}

Just pass in a string, such as C: or D:. It must be just the drive letter and a colon. Also, I made this work for just hard drives. It will not work on CD-ROM drives. It can be expanded, if you need that functionality though.

like image 121
Icemanind Avatar answered Oct 26 '22 17:10

Icemanind


I believe the partition-drive mapping can be read from the Win32_LogicalDiskToPartition and Win32_DiskDriveToDiskPartition classes.

like image 43
fejesjoco Avatar answered Oct 26 '22 17:10

fejesjoco


Win32_DiskDriveToDiskPartition, Win32_LogicalDiskToPartition

Brute force:

  1. Take all disks

    SELECT * FROM Win32_DiskDrive

  2. For each disk get partitions

    ASSOCIATORS OF {Win32_DiskDrive.DeviceID=disk.DeviceID } WHERE AssocClass = Win32_DiskDriveToDiskPartition

  3. For each partition get volume letter

    ASSOCIATORS OF {Win32_DiskPartition.DeviceID=partition.DeviceID} WHERE AssocClass = Win32_LogicalDiskToPartition

like image 45
yesenin Avatar answered Oct 26 '22 17:10

yesenin