Create a gist now

Instantly share code, notes, and snippets.

Embed
C# console app to utilize MSFT_PhysicalDisk
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
class Program
{
static void Main(string[] args)
{
Check();
Console.ReadLine();
}
public static void Check()
{
// Specify Windows Storage Management API namespace.
var scope = new ManagementScope(@"\\.\root\microsoft\windows\storage");
scope.Connect();
var searcher = new ManagementObjectSearcher("SELECT * FROM MSFT_PhysicalDisk");
searcher.Scope = scope;
foreach (var drive in searcher.Get())
{
string deviceId = "";
if (drive["DeviceID"] != null)
deviceId = drive["DeviceID"].ToString();
string model = "";
if (drive["Model"] != null)
model = drive["Model"].ToString();
string busType = "";
ushort busBuff;
if ((drive["BusType"] != null) &&
ushort.TryParse(drive["BusType"].ToString(), out busBuff) &&
busTypeMap.Keys.Contains(busBuff))
busType = busTypeMap[busBuff];
string mediaType = "";
ushort mediaBuff;
if ((drive["MediaType"] != null) &&
ushort.TryParse(drive["MediaType"].ToString(), out mediaBuff) &&
mediaTypeMap.Keys.Contains(mediaBuff))
mediaType = mediaTypeMap[mediaBuff];
string spindleSpeed = "";
uint spindleBuff;
if ((drive["SpindleSpeed"] != null) &&
uint.TryParse(drive["SpindleSpeed"].ToString(), out spindleBuff))
{
if (spindleBuff == UInt32.MaxValue)
spindleSpeed = "unknown";
else if (spindleBuff == 0)
spindleSpeed = "non-rotational media";
else
spindleSpeed = String.Format("{0}RPM", spindleBuff);
}
Console.WriteLine(String.Format("PhysicalDrive:{0} Model:{1}", deviceId, model));
Console.WriteLine(String.Format("BusType:{0} MediaType:{1} SpindleSpeed:{2}", busType, mediaType, spindleSpeed));
Console.WriteLine();
}
}
private static readonly Dictionary<ushort, string> busTypeMap =
new Dictionary<ushort, string>()
{
{0, "unknown"},
{1, "SCSI"},
{2, "ATAPI"},
{3, "ATA"},
{4, "IEEE 1394"},
{5, "SSA"},
{6, "Fibre Channel"},
{7, "USB"},
{8, "RAID"},
{9, "iSCSI"},
{10, "SAS"},
{11, "SATA"},
{12, "SD"},
{13, "MMC"},
{14, "reserved"},
{15, "File-Backed Virtual"},
{16, "Storage Spaces"},
{17, "reserved"},
};
private static readonly Dictionary<ushort, string> mediaTypeMap =
new Dictionary<ushort, string>()
{
{0, "Unspecified"},
{3, "HDD"},
{4, "SSD"},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment