Created
February 25, 2018 14:21
-
-
Save mjs3339/d71feb0b74d4e3d514e3e9c937cc13ce to your computer and use it in GitHub Desktop.
C# Get Detailed Information on Installed Disks
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 DiskInfoStruct | |
{ | |
public string BytesPerSector; | |
public string Capabilities; | |
public string CapabilityDescriptions; | |
public string Caption; | |
public string ConfigManagerErrorCode; | |
public string ConfigManagerUserConfig; | |
public string Description; | |
public string DeviceID; | |
public string Disk; | |
public string FirmwareRevision; | |
public string HorzList; | |
public string Index; | |
public string InterfaceType; | |
public string MediaLoaded; | |
public string MediaType; | |
public string Model; | |
public string Name; | |
public string Partitions; | |
public string PNPDeviceID; | |
public string SCSIBus; | |
public string SCSILogicalUnit; | |
public string SCSIPort; | |
public string SCSITargetId; | |
public string SectorsPerTrack; | |
public string SerialNumber; | |
public string Signature; | |
public string Size; | |
public string Status; | |
public string TotalCylinders; | |
public string TotalHeads; | |
public string TotalSectors; | |
public string TotalTracks; | |
public string TracksPerCylinder; | |
public string VolumesOnDisk; | |
} | |
public static class DiskInformation | |
{ | |
private static Dictionary<string, DiskInfoStruct> DICache = new Dictionary<string, DiskInfoStruct>(); | |
public static Dictionary<string, DiskInfoStruct> DiskInfo | |
{ | |
get | |
{ | |
if (DICache.Count == 0) | |
DICache = GDI(); | |
return DICache; | |
} | |
} | |
//public static List<DiskInfoStruct> ToList(this DiskInfoStruct ds) | |
//{ | |
// var LDS = new List<DiskInfoStruct> {ds}; | |
// return LDS; | |
//} | |
public static void ClearCache() | |
{ | |
if (DICache.Count > 0) | |
DICache.Clear(); | |
} | |
public static string[] GetDisks() | |
{ | |
var Disks = new ArrayList(); | |
foreach (var v in DiskInfo) | |
{ | |
var DiskSize = Convert.ToUInt64(v.Value.Size); | |
var DiskNumber = Convert.ToInt32(v.Value.Disk); | |
Disks.Add(DiskNumber + " (" + v.Value.VolumesOnDisk + ") " + | |
(DiskSize / (1000 * 1000 * 1000)).ToString().InsertCommas() + | |
" GB -" + | |
v.Value.InterfaceType); | |
} | |
return Disks.ToArray(typeof(string)) as string[]; | |
} | |
public static string GetDiskFromDrive(string drive) | |
{ | |
foreach (var v in DiskInfo) | |
{ | |
var voda = v.Value.VolumesOnDisk.Split(','); | |
var vod = ""; | |
foreach (var s in voda) | |
if (s.IndexOf("Hidden", StringComparison.OrdinalIgnoreCase) == -1) | |
vod += s + ","; | |
if (vod.IndexOf(drive, StringComparison.OrdinalIgnoreCase) != -1) | |
return v.Value.Disk; | |
} | |
return null; | |
} | |
//public static DiskInfoStruct GetDiskInfoFromDrive(string drive) | |
//{ | |
// foreach (var v in DiskInfo) | |
// { | |
// var voda = v.Value.VolumesOnDisk.Split(','); | |
// var vod = ""; | |
// foreach (var s in voda) | |
// if (s.IndexOf("Hidden", StringComparison.OrdinalIgnoreCase) == -1) | |
// vod += s + ","; | |
// if (vod.IndexOf(drive, StringComparison.OrdinalIgnoreCase) != -1) | |
// return v.Value; | |
// } | |
// return null; | |
//} | |
//public static DiskInfoStruct GetDiskInfoFromDisk(string disk) | |
//{ | |
// return DiskInfo[disk]; | |
//} | |
private static Dictionary<string, DiskInfoStruct> GDI() | |
{ | |
short DiskCount = 0; | |
var DI = new Dictionary<string, DiskInfoStruct>(); | |
var Win32_DiskDrive_Object = new ManagementObjectSearcher("select * from Win32_DiskDrive"); | |
foreach (var diskobj in Win32_DiskDrive_Object.Get()) | |
{ | |
var dis = new DiskInfoStruct(); | |
var disk = ""; | |
try | |
{ | |
disk = diskobj["Index"].ToString(); | |
dis.Disk = disk; | |
dis.Index = disk; | |
} | |
catch | |
{ | |
continue; | |
} | |
try | |
{ | |
dis.BytesPerSector = diskobj["BytesPerSector"].ToString(); | |
} | |
catch | |
{ | |
continue; | |
} | |
try | |
{ | |
var shortData = (ushort[])diskobj["Capabilities"]; | |
var tstr2 = ""; | |
foreach (var st in shortData) | |
tstr2 += st + " "; | |
dis.Capabilities = tstr2; | |
} | |
catch | |
{ | |
dis.Capabilities = "NA"; | |
} | |
try | |
{ | |
var str = (string[])diskobj["CapabilityDescriptions"]; | |
var str2 = ""; | |
foreach (var st in str) | |
str2 += st + ", "; | |
dis.CapabilityDescriptions = str2; | |
} | |
catch | |
{ | |
dis.CapabilityDescriptions = "NA"; | |
} | |
try | |
{ | |
dis.Caption = diskobj["Caption"].ToString(); | |
} | |
catch | |
{ | |
dis.Caption = "NA"; | |
} | |
try | |
{ | |
dis.ConfigManagerErrorCode = diskobj["ConfigManagerErrorCode"].ToString(); | |
} | |
catch | |
{ | |
dis.ConfigManagerErrorCode = "NA"; | |
} | |
try | |
{ | |
dis.ConfigManagerUserConfig = diskobj["ConfigManagerUserConfig"].ToString(); | |
} | |
catch | |
{ | |
dis.ConfigManagerUserConfig = "NA"; | |
} | |
try | |
{ | |
dis.Description = diskobj["Description"].ToString(); | |
} | |
catch | |
{ | |
dis.Description = "NA"; | |
} | |
try | |
{ | |
dis.DeviceID = diskobj["DeviceID"].ToString(); | |
} | |
catch | |
{ | |
dis.DeviceID = "NA"; | |
} | |
try | |
{ | |
dis.FirmwareRevision = diskobj["FirmwareRevision"].ToString(); | |
} | |
catch | |
{ | |
dis.FirmwareRevision = "NA"; | |
} | |
try | |
{ | |
dis.MediaType = diskobj["MediaType"].ToString(); | |
} | |
catch | |
{ | |
dis.MediaType = "NA"; | |
} | |
try | |
{ | |
dis.MediaLoaded = diskobj["MediaLoaded"].ToString(); | |
} | |
catch | |
{ | |
dis.MediaLoaded = "NA"; | |
} | |
try | |
{ | |
dis.Model = diskobj["Model"].ToString(); | |
} | |
catch | |
{ | |
dis.Model = "NA"; | |
} | |
try | |
{ | |
dis.Name = diskobj["Name"].ToString(); | |
} | |
catch | |
{ | |
dis.Name = "NA"; | |
} | |
try | |
{ | |
dis.InterfaceType = diskobj["InterfaceType"].ToString(); | |
if (dis.Model.IndexOf("SSD", StringComparison.OrdinalIgnoreCase) != -1) | |
dis.InterfaceType = "SSD"; | |
} | |
catch | |
{ | |
dis.InterfaceType = "NA"; | |
} | |
try | |
{ | |
dis.PNPDeviceID = diskobj["PNPDeviceID"].ToString(); | |
} | |
catch | |
{ | |
dis.PNPDeviceID = "NA"; | |
} | |
try | |
{ | |
dis.SCSIBus = diskobj["SCSIBus"].ToString(); | |
} | |
catch | |
{ | |
dis.SCSIBus = "NA"; | |
} | |
try | |
{ | |
dis.SCSILogicalUnit = diskobj["SCSILogicalUnit"].ToString(); | |
} | |
catch | |
{ | |
dis.SCSILogicalUnit = "NA"; | |
} | |
try | |
{ | |
dis.SCSIPort = diskobj["SCSIPort"].ToString(); | |
} | |
catch | |
{ | |
dis.SCSIPort = "NA"; | |
} | |
try | |
{ | |
dis.SCSITargetId = diskobj["SCSITargetId"].ToString(); | |
} | |
catch | |
{ | |
dis.SCSITargetId = "NA"; | |
} | |
try | |
{ | |
dis.SectorsPerTrack = diskobj["SectorsPerTrack"].ToString(); | |
} | |
catch | |
{ | |
dis.SectorsPerTrack = "NA"; | |
} | |
try | |
{ | |
dis.SerialNumber = diskobj["SerialNumber"].ToString(); | |
} | |
catch | |
{ | |
dis.SerialNumber = "NA"; | |
} | |
try | |
{ | |
dis.Signature = diskobj["Signature"].ToString(); | |
} | |
catch | |
{ | |
dis.Signature = "NA"; | |
} | |
try | |
{ | |
dis.Size = diskobj["Size"].ToString(); | |
} | |
catch | |
{ | |
dis.Size = "NA"; | |
} | |
try | |
{ | |
dis.Status = diskobj["Status"].ToString(); | |
} | |
catch | |
{ | |
dis.Status = "NA"; | |
} | |
try | |
{ | |
dis.TotalCylinders = diskobj["TotalCylinders"].ToString(); | |
} | |
catch | |
{ | |
dis.TotalCylinders = "NA"; | |
} | |
try | |
{ | |
dis.TotalHeads = diskobj["TotalHeads"].ToString(); | |
} | |
catch | |
{ | |
dis.TotalHeads = "NA"; | |
} | |
try | |
{ | |
dis.TotalSectors = diskobj["TotalSectors"].ToString(); | |
} | |
catch | |
{ | |
dis.TotalSectors = "NA"; | |
} | |
try | |
{ | |
dis.TotalTracks = diskobj["TotalTracks"].ToString(); | |
} | |
catch | |
{ | |
dis.TotalTracks = "NA"; | |
} | |
try | |
{ | |
dis.TracksPerCylinder = diskobj["TracksPerCylinder"].ToString(); | |
} | |
catch | |
{ | |
dis.TracksPerCylinder = "NA"; | |
} | |
var nop = 0; | |
try | |
{ | |
dis.Partitions = diskobj["Partitions"].ToString(); | |
nop = Convert.ToInt32(dis.Partitions); | |
} | |
catch | |
{ | |
dis.Partitions = "NA"; | |
} | |
try | |
{ | |
dis.VolumesOnDisk = EnumeratePartitions(nop, diskobj); | |
} | |
catch | |
{ | |
dis.VolumesOnDisk = "NA"; | |
} | |
try | |
{ | |
var volumesize = ""; | |
var partition = ""; | |
var p = 0; | |
var pairs = dis.VolumesOnDisk.Split(','); | |
foreach (var v in pairs) | |
{ | |
var drive = v; | |
if (drive.ToUpper().IndexOf("HIDDEN", StringComparison.OrdinalIgnoreCase) != -1) | |
{ | |
volumesize += "0,"; | |
continue; | |
} | |
var kp = new KeyValuePair<string, DriveInfoStruct>(); | |
var found = false; | |
foreach (var d in GatherInternalDriveInformation()) | |
if (d.Key == drive) | |
{ | |
found = true; | |
kp = d; | |
break; | |
} | |
if (found) | |
{ | |
volumesize += kp.Value.Size + ","; | |
partition += p + ","; | |
p++; | |
} | |
else | |
{ | |
volumesize += "0,"; | |
partition += "0,"; | |
p++; | |
} | |
} | |
dis.HorzList = dis.Disk + ":" + | |
dis.Size + ":" + | |
dis.VolumesOnDisk + ",:" + | |
volumesize + ":" + | |
dis.Partitions + ":" + | |
dis.Model + ":" + | |
partition + ":"; | |
} | |
catch (Exception e) | |
{ | |
var err = e.Message; | |
ExceptionLog.ExLog(e, "DiskInformation", "GDI"); | |
} | |
DI.Add(disk, dis); | |
DiskCount++; | |
} | |
if (DiskCount == 0) | |
MessageBox.Show(@"There are no installed disks detected.", | |
"Error", MessageBoxButtons.OK, | |
MessageBoxIcon.Error); | |
return DI; | |
} | |
private static string EnumeratePartitions(int nop, ManagementBaseObject obj) | |
{ | |
var DriveBuildString = string.Empty; | |
for (var i = 0; i < nop; i++) | |
{ | |
ManagementObject partition = null; | |
try | |
{ | |
partition = | |
GetPartition(new ManagementObjectSearcher($"associators of {{Win32_DiskDrive.DeviceID='{obj["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition"), i + 1); | |
} | |
catch | |
{ | |
partition = null; | |
} | |
if (partition != null) | |
{ | |
ManagementObject logical = null; | |
try | |
{ | |
logical = GetPartition(new ManagementObjectSearcher($"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition"), 1); | |
} | |
catch | |
{ | |
logical = null; | |
} | |
if (logical == null) | |
DriveBuildString = DelimPack("Hidden", | |
DriveBuildString, ","); | |
else | |
try | |
{ | |
DriveBuildString = | |
DelimPack( | |
logical["Name"] | |
.ToString() | |
.Substring(0, 1), | |
DriveBuildString, ","); | |
} | |
catch | |
{ | |
DriveBuildString = DelimPack("-", | |
DriveBuildString, ","); | |
} | |
} | |
} | |
return DriveBuildString; | |
} | |
private static string DelimPack(string input, string output, string separator) | |
{ | |
if (output == string.Empty) | |
output += input; | |
else | |
output += separator + input; | |
return output; | |
} | |
private static ManagementObject GetPartition(ManagementObjectSearcher searcher, int c) | |
{ | |
ManagementObject result = null; | |
var n = 0; | |
foreach (ManagementObject item in searcher.Get()) | |
{ | |
n++; | |
if (n == c) | |
{ | |
result = item; | |
break; | |
} | |
} | |
return result; | |
} | |
private static Dictionary<string, bool> BuildReadyList() | |
{ | |
var ReadyList = new Dictionary<string, bool>(); | |
foreach (var driveInfo in DriveInfo.GetDrives()) | |
ReadyList.Add(driveInfo.Name.Substring(0, 1), driveInfo.IsReady); | |
return ReadyList; | |
} | |
private static Dictionary<string, DriveInfoStruct> GatherInternalDriveInformation() | |
{ | |
var DI = new Dictionary<string, DriveInfoStruct>(); | |
var ReadyList = BuildReadyList(); | |
var Win32_LogicalDisk_Object = new ManagementObjectSearcher("select * from Win32_LogicalDisk"); | |
foreach (ManagementObject diskobj in Win32_LogicalDisk_Object.Get()) | |
{ | |
var dis = new DriveInfoStruct(); | |
var Name = ""; | |
try | |
{ | |
Name = diskobj["Name"].ToString().Substring(0, 1); | |
dis.Drive = Name; | |
} | |
catch | |
{ | |
continue; | |
} | |
if (!ReadyList[Name]) | |
continue; | |
try | |
{ | |
dis.Access = diskobj["Access"].ToString(); | |
} | |
catch | |
{ | |
dis.Access = "NA"; | |
} | |
try | |
{ | |
dis.Compressed = diskobj["Compressed"].ToString(); | |
} | |
catch | |
{ | |
dis.Compressed = "NA"; | |
} | |
try | |
{ | |
dis.Description = diskobj["Description"].ToString(); | |
} | |
catch | |
{ | |
dis.Description = "NA"; | |
} | |
try | |
{ | |
dis.DriveType = diskobj["DriveType"].ToString(); | |
} | |
catch | |
{ | |
dis.DriveType = "NA"; | |
} | |
try | |
{ | |
dis.FileSystem = diskobj["FileSystem"].ToString(); | |
} | |
catch | |
{ | |
dis.FileSystem = "NA"; | |
} | |
try | |
{ | |
dis.FreeSpace = diskobj["FreeSpace"].ToString(); | |
} | |
catch | |
{ | |
dis.FreeSpace = "NA"; | |
} | |
try | |
{ | |
dis.MaximumComponentLength = diskobj["MaximumComponentLength"].ToString(); | |
} | |
catch | |
{ | |
dis.MaximumComponentLength = "NA"; | |
} | |
try | |
{ | |
dis.MediaType = diskobj["MediaType"].ToString(); | |
} | |
catch | |
{ | |
dis.MediaType = "NA"; | |
} | |
try | |
{ | |
dis.Size = diskobj["Size"].ToString(); | |
} | |
catch | |
{ | |
dis.Size = "NA"; | |
} | |
try | |
{ | |
dis.VolumeDirty = diskobj["VolumeDirty"].ToString(); | |
} | |
catch | |
{ | |
dis.VolumeDirty = "NA"; | |
} | |
try | |
{ | |
dis.VolumeName = diskobj["VolumeName"].ToString(); | |
} | |
catch | |
{ | |
dis.VolumeName = "NA"; | |
} | |
try | |
{ | |
dis.VolumeSerialNumber = diskobj["VolumeSerialNumber"].ToString(); | |
} | |
catch | |
{ | |
dis.VolumeSerialNumber = "NA"; | |
} | |
dis.HorzList = dis.Drive + ":" + | |
dis.Size + ":" + | |
dis.FreeSpace + ":" + | |
dis.Description + ":"; | |
DI.Add(Name, dis); | |
} | |
return DI; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment