Created
June 18, 2018 17:36
-
-
Save mjs3339/f11620658e258b107f88fc59a6b980f8 to your computer and use it in GitHub Desktop.
C# Get Physical Disk Information using WMI and MSFT_PhysicalDisk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Msft_PhysicalDisk use MediaType “MediaTypes = {"Unspecified", "HDD", "SSD", "SCM"};”. If this will not work use “Win32_DiskDrive” and parse the “Model” field for “SSD” or use the “Model” data to search the Internet for the “SSD” indication.
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2Ff11620658e258b107f88fc59a6b980f8%3Femail_source%3Dnotifications%26email_token%3DAIJKBTAJ7Z3OLSXVU7HOHBLQXZMT5A5CNFSM4JYKSZP2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF5TBU%23gistcomment-3106330&data=02%7C01%7C%7C87d9fd6cdd0044a7901308d77cb8a5ae%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637115004796029678&sdata=vj65pD3r%2B75WPQrhN1j29O9dETMJVDfoeFZc4%2FqTuiY%3D&reserved=0>, or unsubscribe<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAIJKBTAKMESVE4QGCMWUN73QXZMT5ANCNFSM4JYKSZPQ&data=02%7C01%7C%7C87d9fd6cdd0044a7901308d77cb8a5ae%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637115004796039683&sdata=Why7McBAUtwtkw%2FhMFzvGbLM4n9ZNqeTk%2BDajT2RG7s%3D&reserved=0>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.