Skip to content

Instantly share code, notes, and snippets.

@MiloszKrajewski
MiloszKrajewski / GetDrives.cs
Created March 29, 2016 08:29
Get drive information (C#, Windows, WMI)
var driveQuery = new ManagementObjectSearcher("select * from Win32_DiskDrive");
foreach (ManagementObject d in driveQuery.Get())
{
var deviceId = d.Properties["DeviceId"].Value;
//Console.WriteLine("Device");
//Console.WriteLine(d);
var partitionQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_DiskDriveToDiskPartition", d.Path.RelativePath);
var partitionQuery = new ManagementObjectSearcher(partitionQueryText);
foreach (ManagementObject p in partitionQuery.Get())
{