How to read HardDisk Temperature?

不羁岁月 提交于 2019-12-05 00:15:28
Stecya

Here is code snippet from this article Hope it helps

//S.M.A.R.T.  Temperature attribute
const byte TEMPERATURE_ATTRIBUTE = 194;

public List<byte> GetDriveTemp()
{
    var retval = new List<byte>();
    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;
}

use VMI and MSStorageDriver_ATAPISmartData to get VendorSpecific byte array and 115 byte number is temperature. Why 115? More here.

Code partly generated with VMI Code Creator

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSStorageDriver_ATAPISmartData");

foreach (ManagementObject queryObj in searcher.Get())
{
   if (queryObj["VendorSpecific"] != null)
   {
       byte[] arrVendorSpecific = (byte[])(queryObj["VendorSpecific"]);
       string temp = arrVendorSpecific[115].ToString();
    }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!