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.
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.
I believe the partition-drive mapping can be read from the Win32_LogicalDiskToPartition
and Win32_DiskDriveToDiskPartition
classes.
Win32_DiskDriveToDiskPartition, Win32_LogicalDiskToPartition
Brute force:
Take all disks
SELECT * FROM Win32_DiskDrive
For each disk get partitions
ASSOCIATORS OF {Win32_DiskDrive.DeviceID=disk.DeviceID } WHERE AssocClass = Win32_DiskDriveToDiskPartition
For each partition get volume letter
ASSOCIATORS OF {Win32_DiskPartition.DeviceID=partition.DeviceID} WHERE AssocClass = Win32_LogicalDiskToPartition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With