Created
February 25, 2018 14:31
-
-
Save mjs3339/6dbe07655a8244aa4c08ce8143af890f to your computer and use it in GitHub Desktop.
C# Get Detailed Information on Installed Drives/Volumes
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 DriveInfoStruct | |
{ | |
public string Access; | |
public string Caption; | |
public string Compressed; | |
public string Description; | |
public string DevicePath; | |
public List<DiskInfoStruct> DiskInformations = new List<DiskInfoStruct>(); | |
public string DiskNumbers; | |
public string Drive; | |
public string DriveType; | |
public string ExtentLengths; | |
public string FileSystem; | |
public string FreeSpace; | |
public string HorzList; | |
public string MaximumComponentLength; | |
public string MediaType; | |
public string Name; | |
public string QuotasDisabled; | |
public string QuotasIncomplete; | |
public string QuotasRebuilding; | |
public string Size; | |
public string StartingOffsets; | |
public string SupportsDiskQuotas; | |
public string SupportsFileBasedCompression; | |
public string VolumeDirty; | |
public string VolumeMountPoint; | |
public string VolumeName; | |
public string VolumeSerialNumber; | |
public bool IsUsb | |
{ | |
get | |
{ | |
var ret = false; | |
if (string.IsNullOrEmpty(Drive)) | |
return false; | |
try | |
{ | |
var i = Drive.ToUpper().Substring(0, 1); | |
ret = DriveInformation.DriveInfos[i].DiskInformations[0].InterfaceType.IndexOf("USB", StringComparison.OrdinalIgnoreCase) != -1; | |
} | |
catch | |
{ | |
} | |
return ret; | |
} | |
} | |
} | |
public static class DriveInformation | |
{ | |
private static Dictionary<string, DriveInfoStruct> _diCache = new Dictionary<string, DriveInfoStruct>(); | |
public static Dictionary<string, DriveInfoStruct> DriveInfos | |
{ | |
get | |
{ | |
if (_diCache.Count == 0) | |
_diCache = GDI(); | |
return _diCache; | |
} | |
} | |
public static void ClearCache() | |
{ | |
if (_diCache.Count > 0) | |
_diCache.Clear(); | |
} | |
public static string[] GetDrives() | |
{ | |
var Drives = new ArrayList(); | |
foreach (var v in DriveInfos) | |
{ | |
var Size = Convert.ToUInt64(v.Value.Size); | |
Drives.Add($"{v.Value.Drive} → {Size.FormatSize(true)}"); | |
} | |
return Drives.ToArray(typeof(string)) as string[]; | |
} | |
public static string[] GetDisks() | |
{ | |
var Disks = new ArrayList(); | |
foreach (var v in DriveInfos) | |
{ | |
var DiskSize = Convert.ToUInt64(v.Value.ExtentLengths[0]); | |
var DiskNumber = Convert.ToInt32(v.Value.DiskNumbers[0]); | |
Disks.Add($"{DiskNumber} → {DiskSize.FormatSize(true)}"); | |
} | |
return Disks.ToArray(typeof(string)) as string[]; | |
} | |
public static List<string> GetDrivesReadyList() | |
{ | |
var rl = new List<string>(); | |
foreach (var driveInfo in DriveInfo.GetDrives()) | |
if (driveInfo.IsReady) | |
rl.Add(driveInfo.Name.Substring(0, 1)); | |
return rl; | |
} | |
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> GDI() | |
{ | |
var DI = new Dictionary<string, DriveInfoStruct>(); | |
var sudi = new SetupDi(); | |
var vp = sudi.GetVolumePaths(); | |
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; | |
dis.Name = Name; | |
} | |
catch | |
{ | |
continue; | |
} | |
if (!ReadyList[Name]) | |
continue; | |
try | |
{ | |
dis.Access = diskobj["Access"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.Access = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.Caption = diskobj["Caption"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.Caption = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.Compressed = diskobj["Compressed"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.Compressed = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.Description = diskobj["Description"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.Description = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.DriveType = diskobj["DriveType"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.DriveType = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.FileSystem = diskobj["FileSystem"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.FileSystem = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.FreeSpace = diskobj["FreeSpace"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.FreeSpace = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.MaximumComponentLength = diskobj["MaximumComponentLength"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.MaximumComponentLength = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.MediaType = diskobj["MediaType"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.MediaType = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.QuotasDisabled = diskobj["QuotasDisabled"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.QuotasDisabled = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.QuotasIncomplete = diskobj["QuotasIncomplete"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.QuotasIncomplete = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.QuotasRebuilding = diskobj["QuotasRebuilding"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.QuotasRebuilding = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.Size = diskobj["Size"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.Size = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.SupportsDiskQuotas = diskobj["SupportsDiskQuotas"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.SupportsDiskQuotas = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.SupportsFileBasedCompression = diskobj["SupportsFileBasedCompression"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.SupportsFileBasedCompression = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.VolumeDirty = diskobj["VolumeDirty"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.VolumeDirty = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.VolumeName = diskobj["VolumeName"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.VolumeName = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
try | |
{ | |
dis.VolumeSerialNumber = diskobj["VolumeSerialNumber"].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
dis.VolumeSerialNumber = "NA"; | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
dis.HorzList = dis.Drive + ":" + | |
dis.Size + ":" + | |
dis.FreeSpace + ":" + | |
dis.Description + ":"; | |
try | |
{ | |
var idx = $"{dis.Drive}:"; | |
dis.VolumeMountPoint = vp[idx].VolumeMountPoint; | |
dis.DevicePath = vp[idx].DevicePath; | |
foreach (var v in vp[idx].DiskNumbers) | |
dis.DiskNumbers += $"{v},"; | |
foreach (var v in vp[idx].StartingOffsets) | |
dis.StartingOffsets += $"{v},"; | |
foreach (var v in vp[idx].ExtentLengths) | |
dis.ExtentLengths += $"{v},"; | |
foreach (var v in vp[idx].DiskNumbers) | |
dis.DiskInformations.Add(DiskInformation.DiskInfo[$"{v}"]); | |
} | |
catch (Exception ex) | |
{ | |
ExceptionLog.ExLog(ex, "GDI", "DriveInformation"); | |
} | |
DI.Add(Name, dis); | |
} | |
return DI; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment