Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created June 18, 2018 17:36
Show Gist options
  • Save mjs3339/f11620658e258b107f88fc59a6b980f8 to your computer and use it in GitHub Desktop.
Save mjs3339/f11620658e258b107f88fc59a6b980f8 to your computer and use it in GitHub Desktop.
C# Get Physical Disk Information using WMI and MSFT_PhysicalDisk
public class PDiskInformation
{
private Dictionary<string, PhysicalDiskStruct> _pdiCache = new Dictionary<string, PhysicalDiskStruct>();
public Dictionary<string, PhysicalDiskStruct> PDiskInfo
{
get
{
if(_pdiCache.Count == 0)
_pdiCache = GetDiskPI();
return _pdiCache;
}
}
public void ClearCache()
{
if(_pdiCache.Count > 0)
_pdiCache.Clear();
}
private static Dictionary<string, PhysicalDiskStruct> GetDiskPI()
{
var DI = new Dictionary<string, PhysicalDiskStruct>();
var scope = new ManagementScope(@"\\localhost\ROOT\Microsoft\Windows\Storage");
var query = new ObjectQuery("SELECT * FROM MSFT_PhysicalDisk");
var searcher = new ManagementObjectSearcher(scope, query);
var dObj = searcher.Get();
var wobj = new ManagementObjectSearcher("select * from MSFT_PhysicalDisk");
foreach(ManagementObject diskobj in dObj)
{
var dis = new PhysicalDiskStruct();
try
{
dis.SupportedUsages = (ushort[]) diskobj["SupportedUsages"];
}
catch(Exception ex)
{
dis.SupportedUsages = null;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.CannotPoolReason = (ushort[]) diskobj["CannotPoolReason"];
}
catch(Exception ex)
{
dis.CannotPoolReason = null;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.OperationalStatus = (ushort[]) diskobj["OperationalStatus"];
}
catch(Exception ex)
{
dis.OperationalStatus = null;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.OperationalDetails = (string[]) diskobj["OperationalDetails"];
}
catch(Exception ex)
{
dis.OperationalDetails = null;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.UniqueIdFormat = (ushort) diskobj["UniqueIdFormat"];
}
catch(Exception ex)
{
dis.UniqueIdFormat = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.DeviceId = diskobj["DeviceId"].ToString();
}
catch(Exception ex)
{
dis.DeviceId = "NA";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.FriendlyName = (string) diskobj["FriendlyName"];
}
catch(Exception ex)
{
dis.FriendlyName = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.HealthStatus = (ushort) diskobj["HealthStatus"];
}
catch(Exception ex)
{
dis.HealthStatus = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.PhysicalLocation = (string) diskobj["PhysicalLocation"];
}
catch(Exception ex)
{
dis.PhysicalLocation = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.VirtualDiskFootprint = (ushort) diskobj["VirtualDiskFootprint"];
}
catch(Exception ex)
{
dis.VirtualDiskFootprint = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.Usage = (ushort) diskobj["Usage"];
}
catch(Exception ex)
{
dis.Usage = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.Description = (string) diskobj["Description"];
}
catch(Exception ex)
{
dis.Description = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.PartNumber = (string) diskobj["PartNumber"];
}
catch(Exception ex)
{
dis.PartNumber = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.FirmwareVersion = (string) diskobj["FirmwareVersion"];
}
catch(Exception ex)
{
dis.FirmwareVersion = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.SoftwareVersion = (string) diskobj["SoftwareVersion"];
}
catch(Exception ex)
{
dis.SoftwareVersion = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.Size = (ulong) diskobj["SoftwareVersion"];
}
catch(Exception ex)
{
dis.Size = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.AllocatedSize = (ulong) diskobj["AllocatedSize"];
}
catch(Exception ex)
{
dis.AllocatedSize = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.BusType = (ushort) diskobj["BusType"];
}
catch(Exception ex)
{
dis.BusType = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.IsWriteCacheEnabled = (bool) diskobj["IsWriteCacheEnabled"];
}
catch(Exception ex)
{
dis.IsWriteCacheEnabled = false;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.IsPowerProtected = (bool) diskobj["IsPowerProtected"];
}
catch(Exception ex)
{
dis.IsPowerProtected = false;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.PhysicalSectorSize = (ulong) diskobj["PhysicalSectorSize"];
}
catch(Exception ex)
{
dis.PhysicalSectorSize = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.LogicalSectorSize = (ulong) diskobj["LogicalSectorSize"];
}
catch(Exception ex)
{
dis.LogicalSectorSize = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.SpindleSpeed = (uint) diskobj["SpindleSpeed"];
}
catch(Exception ex)
{
dis.SpindleSpeed = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.IsIndicationEnabled = (bool) diskobj["IsIndicationEnabled"];
}
catch(Exception ex)
{
dis.IsIndicationEnabled = false;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.EnclosureNumber = (ushort) diskobj["EnclosureNumber"];
}
catch(Exception ex)
{
dis.EnclosureNumber = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.SlotNumber = (ushort) diskobj["SlotNumber"];
}
catch(Exception ex)
{
dis.SlotNumber = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.CanPool = (bool) diskobj["CanPool"];
}
catch(Exception ex)
{
dis.CanPool = false;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.OtherCannotPoolReasonDescription = (string) diskobj["OtherCannotPoolReasonDescription"];
}
catch(Exception ex)
{
dis.OtherCannotPoolReasonDescription = "?";
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.IsPartial = (bool) diskobj["IsPartial"];
}
catch(Exception ex)
{
dis.IsPartial = false;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
try
{
dis.MediaType = (ushort) diskobj["MediaType"];
}
catch(Exception ex)
{
dis.MediaType = 0;
ExceptionLog.ExLog(ex, "GetDiskPI", "PhysicalDiskStruct");
}
DI.Add(dis.DeviceId, dis);
}
return DI;
}
}
public class PhysicalDiskStruct
{
public ulong AllocatedSize;
public ushort BusType;
public ushort[] CannotPoolReason;
public bool CanPool;
public string Description;
public string DeviceId;
public ushort EnclosureNumber;
public string FirmwareVersion;
public string FriendlyName;
public ushort HealthStatus;
public bool IsIndicationEnabled;
public bool IsPartial;
public bool IsPowerProtected;
public bool IsWriteCacheEnabled;
public ulong LogicalSectorSize;
public ushort MediaType;
public string[] OperationalDetails;
public ushort[] OperationalStatus;
public string OtherCannotPoolReasonDescription;
public string PartNumber;
public string PhysicalLocation;
public ulong PhysicalSectorSize;
public ulong Size;
public ushort SlotNumber;
public string SoftwareVersion;
public uint SpindleSpeed;
public ushort[] SupportedUsages;
public ushort UniqueIdFormat;
public ushort Usage;
public ushort VirtualDiskFootprint;
}
@nikhil-bst
Copy link

Hi,

How can I use Msft_PhysicalDisk for External Disk Drives? For example, I want to detect whether a disk drive is SSD or not?

Thanks.

@mjs3339
Copy link
Author

mjs3339 commented Dec 9, 2019 via email

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