# HG changeset patch # User moel.mich # Date 1342980431 0 # Node ID 5077ed7ddca8e2489a3ac23e196f7e9a1ae9141c # Parent 1036b453f1f6f7281c49b49aa65796b0f08e9429 Added used space load sensors for hard drives. diff -r 1036b453f1f6 -r 5077ed7ddca8 Hardware/HDD/AbstractHarddrive.cs --- a/Hardware/HDD/AbstractHarddrive.cs Wed Jul 18 22:47:30 2012 +0000 +++ b/Hardware/HDD/AbstractHarddrive.cs Sun Jul 22 18:07:11 2012 +0000 @@ -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 @@ 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 @@ 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 @@ 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 @@ 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 @@ } 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 @@ 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 @@ 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 -r 1036b453f1f6 -r 5077ed7ddca8 Hardware/HDD/DebugSmart.cs --- a/Hardware/HDD/DebugSmart.cs Wed Jul 18 22:47:30 2012 +0000 +++ b/Hardware/HDD/DebugSmart.cs Sun Jul 22 18:07:11 2012 +0000 @@ -412,6 +412,10 @@ } public IntPtr InvalidHandle { get { return (IntPtr)(-1); } } + + public string[] GetLogicalDrives(int driveIndex) { + return new string[0]; + } } #endif diff -r 1036b453f1f6 -r 5077ed7ddca8 Hardware/HDD/ISmart.cs --- a/Hardware/HDD/ISmart.cs Wed Jul 18 22:47:30 2012 +0000 +++ b/Hardware/HDD/ISmart.cs Sun Jul 22 18:07:11 2012 +0000 @@ -30,5 +30,7 @@ void CloseHandle(IntPtr handle); IntPtr InvalidHandle { get; } + + string[] GetLogicalDrives(int driveIndex); } } diff -r 1036b453f1f6 -r 5077ed7ddca8 Hardware/HDD/WindowsSmart.cs --- a/Hardware/HDD/WindowsSmart.cs Wed Jul 18 22:47:30 2012 +0000 +++ b/Hardware/HDD/WindowsSmart.cs Sun Jul 22 18:07:11 2012 +0000 @@ -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 @@ 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 -r 1036b453f1f6 -r 5077ed7ddca8 Properties/AssemblyVersion.cs --- a/Properties/AssemblyVersion.cs Wed Jul 18 22:47:30 2012 +0000 +++ b/Properties/AssemblyVersion.cs Sun Jul 22 18:07:11 2012 +0000 @@ -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