HOWTO: Get HardDisk Temperature with .NET

Hard disk temperature can be obtained using WMI calls in .NET. The temperature can be retrieved from the Vendor Specific array, in the MSStorageDriver_ATAPISmartData class. This array stores the information for each Vendor differently. So we have to search for the temperature attribute(194 or Hex C2) in the array and retrieve the temperature value.

This following code works on most models with S.M.A.R.T capabilities including Samsung, Seagate, IBM (Hitachi), Fujitsu (not all models), Maxtor, WD (Western Digital) (not all models).

C#

//S.M.A.R.T.  Temperature attritube
const byte TEMPERATURE_ATTRIBUTE = 194;
public List GetDriveTemp()
{
	List retval = new List();
	try
	{
		ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData");
                //loop through all the hard disks
		foreach (ManagementObject queryObj in searcher.Get())
		{
			byte[] arrVendorSpecific = (byte[])queryObj.GetPropertyValue("VendorSpecific");
			//Find the temperature attribute
                        int tempIndex = Array.IndexOf(arrVendorSpecific, TEMPERATURE_ATTRIBUTE);
			retval.Add(arrVendorSpecific[tempIndex + 5]);
		}
	}
	catch (ManagementException err)
	{
		Console.WriteLine("An error occurred while querying for WMI data: " + err.Message);
	}
	return retval;
}

VB .NET

'S.M.A.R.T.  Temperature attritube
Const TEMPERATURE_ATTRIBUTE As Byte = 194
Public Function GetDriveTemp() As List
	Dim retval As New List()
	Try
		Dim searcher As New ManagementObjectSearcher("root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData")
		'loop through all the hard disks
		For Each queryObj As ManagementObject In searcher.[Get]()
			Dim arrVendorSpecific As Byte() = DirectCast(queryObj.GetPropertyValue("VendorSpecific"), Byte())
			'Find the temperature attribute
			Dim tempIndex As Integer = Array.IndexOf(arrVendorSpecific, TEMPERATURE_ATTRIBUTE)
			retval.Add(arrVendorSpecific(tempIndex + 5))
		Next
	Catch err As ManagementException
		Console.WriteLine("An error occurred while querying for WMI data: " + err.Message)
	End Try
	Return retval
End Function

References: Temperature – Attribute information

2 thoughts on “HOWTO: Get HardDisk Temperature with .NET

Leave a comment