diff --git a/Hardware/HDD/AbstractHarddrive.cs b/Hardware/HDD/AbstractHarddrive.cs index 6e0efe4..ab4564c 100644 --- a/Hardware/HDD/AbstractHarddrive.cs +++ b/Hardware/HDD/AbstractHarddrive.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Text; using OpenHardwareMonitor.Collections; @@ -42,6 +43,9 @@ namespace OpenHardwareMonitor.Hardware.HDD { private IList smartAttributes; private IDictionary sensors; + private DriveInfo[] driveInfos; + private Sensor usageSensor; + protected AbstractHarddrive(ISmart smart, string name, string firmwareRevision, int index, IEnumerable smartAttributes, ISettings settings) @@ -59,6 +63,17 @@ namespace OpenHardwareMonitor.Hardware.HDD { this.smartAttributes = new List(smartAttributes); + string[] logicalDrives = smart.GetLogicalDrives(index); + List driveInfoList = new List(logicalDrives.Length); + foreach (string logicalDrive in logicalDrives) { + try { + DriveInfo di = new DriveInfo(logicalDrive); + if (di.TotalSize > 0) + driveInfoList.Add(new DriveInfo(logicalDrive)); + } catch (ArgumentException) { } catch (IOException) { } + } + driveInfos = driveInfoList.ToArray(); + CreateSensors(); } @@ -83,7 +98,7 @@ namespace OpenHardwareMonitor.Hardware.HDD { smart.CloseHandle(deviceHandle); if (!nameValid || string.IsNullOrEmpty(name)) - return null; + return null; foreach (Type type in hddTypes) { // get the array of name prefixes for the current type @@ -161,6 +176,12 @@ namespace OpenHardwareMonitor.Hardware.HDD { sensorTypeAndChannels.Add(pair); } } + + if (driveInfos.Length > 0) { + usageSensor = + new Sensor("Used Space", 0, SensorType.Load, this, settings); + ActivateSensor(usageSensor); + } } public override HardwareType HardwareType { @@ -184,6 +205,16 @@ namespace OpenHardwareMonitor.Hardware.HDD { } UpdateAdditionalSensors(values); + + if (usageSensor != null) { + long totalSize = 0; + long totalFreeSpace = 0; + for (int i = 0; i < driveInfos.Length; i++) { + totalSize += driveInfos[i].TotalSize; + totalFreeSpace += driveInfos[i].TotalFreeSpace; + } + usageSensor.Value = 100.0f - (100.0f * totalFreeSpace) / totalSize; + } } count++; @@ -201,7 +232,7 @@ namespace OpenHardwareMonitor.Hardware.HDD { r.AppendLine(); r.AppendLine("Drive name: " + name); r.AppendLine("Firmware version: " + firmwareRevision); - r.AppendLine(); + r.AppendLine(); r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}{6}{7}", ("ID").PadRight(3), @@ -253,6 +284,14 @@ namespace OpenHardwareMonitor.Hardware.HDD { r.AppendLine(); } + foreach (DriveInfo di in driveInfos) { + r.AppendLine("Logical drive name: " + di.Name); + r.AppendLine("Format: " + di.DriveFormat); + r.AppendLine("Total size: " + di.TotalSize); + r.AppendLine("Total free space: " + di.TotalFreeSpace); + r.AppendLine(); + } + return r.ToString(); } diff --git a/Hardware/HDD/DebugSmart.cs b/Hardware/HDD/DebugSmart.cs index 5977dac..9140e78 100644 --- a/Hardware/HDD/DebugSmart.cs +++ b/Hardware/HDD/DebugSmart.cs @@ -412,6 +412,10 @@ namespace OpenHardwareMonitor.Hardware.HDD { } public IntPtr InvalidHandle { get { return (IntPtr)(-1); } } + + public string[] GetLogicalDrives(int driveIndex) { + return new string[0]; + } } #endif diff --git a/Hardware/HDD/ISmart.cs b/Hardware/HDD/ISmart.cs index 719b00a..811bdb2 100644 --- a/Hardware/HDD/ISmart.cs +++ b/Hardware/HDD/ISmart.cs @@ -30,5 +30,7 @@ namespace OpenHardwareMonitor.Hardware.HDD { void CloseHandle(IntPtr handle); IntPtr InvalidHandle { get; } + + string[] GetLogicalDrives(int driveIndex); } } diff --git a/Hardware/HDD/WindowsSmart.cs b/Hardware/HDD/WindowsSmart.cs index 254d65d..5653632 100644 --- a/Hardware/HDD/WindowsSmart.cs +++ b/Hardware/HDD/WindowsSmart.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; +using System.Management; using System.Runtime.InteropServices; namespace OpenHardwareMonitor.Hardware.HDD { @@ -341,6 +342,23 @@ namespace OpenHardwareMonitor.Hardware.HDD { NativeMethods.CloseHandle(handle); } + public string[] GetLogicalDrives(int driveIndex) { + List list = new List(); + try { + using (ManagementObjectSearcher s = new ManagementObjectSearcher( + "root\\CIMV2", + "SELECT * FROM Win32_DiskPartition " + + "WHERE DiskIndex = " + driveIndex)) + using (ManagementObjectCollection dpc = s.Get()) + foreach (ManagementObject dp in dpc) + using (ManagementObjectCollection ldc = + dp.GetRelated("Win32_LogicalDisk")) + foreach (ManagementBaseObject ld in ldc) + list.Add(((string)ld["Name"]).TrimEnd(':')); + } catch { } + return list.ToArray(); + } + protected static class NativeMethods { private const string KERNEL = "kernel32.dll"; diff --git a/Properties/AssemblyVersion.cs b/Properties/AssemblyVersion.cs index 0da07fa..b9f9cc2 100644 --- a/Properties/AssemblyVersion.cs +++ b/Properties/AssemblyVersion.cs @@ -10,5 +10,5 @@ using System.Reflection; -[assembly: AssemblyVersion("0.4.0.20")] -[assembly: AssemblyInformationalVersion("0.4.0.20 Alpha")] \ No newline at end of file +[assembly: AssemblyVersion("0.4.0.21")] +[assembly: AssemblyInformationalVersion("0.4.0.21 Alpha")] \ No newline at end of file