Home > Programming > C# Get Physical Device ID from Drive Letter or Logical Disk ID

C# Get Physical Device ID from Drive Letter or Logical Disk ID

var logicalDiskId = "C:";
var deviceId = string.Empty;

var query = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" + logicalDiskId + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
var queryResults = new ManagementObjectSearcher(query);
var partitions = queryResults.Get();

foreach (var partition in partitions)
{
query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
queryResults = new ManagementObjectSearcher(query);
var drives = queryResults.Get();

foreach (var drive in drives)
deviceId = drive["DeviceID"].ToString();
}
Console.WriteLine(deviceId);
Advertisements
Categories: Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: