Added used space load sensors for hard drives.

This commit is contained in:
Michael Möller 2012-07-22 18:07:11 +00:00
parent a53679c3ad
commit 5e87af437d
5 changed files with 67 additions and 4 deletions

View File

@ -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<SmartAttribute> smartAttributes;
private IDictionary<SmartAttribute, Sensor> sensors;
private DriveInfo[] driveInfos;
private Sensor usageSensor;
protected AbstractHarddrive(ISmart smart, string name,
string firmwareRevision, int index,
IEnumerable<SmartAttribute> smartAttributes, ISettings settings)
@ -59,6 +63,17 @@ namespace OpenHardwareMonitor.Hardware.HDD {
this.smartAttributes = new List<SmartAttribute>(smartAttributes);
string[] logicalDrives = smart.GetLogicalDrives(index);
List<DriveInfo> driveInfoList = new List<DriveInfo>(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();
}

View File

@ -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

View File

@ -30,5 +30,7 @@ namespace OpenHardwareMonitor.Hardware.HDD {
void CloseHandle(IntPtr handle);
IntPtr InvalidHandle { get; }
string[] GetLogicalDrives(int driveIndex);
}
}

View File

@ -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<string> list = new List<string>();
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";

View File

@ -10,5 +10,5 @@
using System.Reflection;
[assembly: AssemblyVersion("0.4.0.20")]
[assembly: AssemblyInformationalVersion("0.4.0.20 Alpha")]
[assembly: AssemblyVersion("0.4.0.21")]
[assembly: AssemblyInformationalVersion("0.4.0.21 Alpha")]