Skip to content

Instantly share code, notes, and snippets.

@emoacht
Last active November 19, 2023 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoacht/20ce2736d8f5dda56201 to your computer and use it in GitHub Desktop.
Save emoacht/20ce2736d8f5dda56201 to your computer and use it in GitHub Desktop.
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, "NVMe"},
{18, "Reserved"},
};
private static readonly Dictionary<ushort, string> mediaTypeMap =
new Dictionary<ushort, string>()
{
{0, "Unspecified"},
{3, "HDD"},
{4, "SSD"},
{5, "SCM"},
};
}
@nikhil-bst
Copy link

How can I do this for external disks?

@emoacht
Copy link
Author

emoacht commented Dec 6, 2019

It can work with external storage.
For any specific question, maybe you can ask at SO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment