I am working on a project which supposed to include computer's fans status. Most of the properties I need can be acquired from the Win32_Fan
class. Sadly, I couldn't find a way to use this class for getting the current reading of the fan speed. In the Win32_Fan MSDN page it is mentioned in the "DesiredSpeed" property that the current speed is determined by a sensor named CIM_Tachometer
:
DesiredSpeed
Data type: uint64
Access type: Read-only
Qualifiers: Units ("revolutions per minute")
Currently requested fan speed, defined in revolutions per minute, when a variable speed fan is supported (VariableSpeed is TRUE). The current speed is determined by a sensor (CIM_Tachometer) that is associated with the fan using the CIM_AssociatedSensor relationship.
This property is inherited from CIM_Fan.
For more information about using uint64 values in scripts, see Scripting in WMI.
After I saw that, I searched for this Tachometer CIM sensor and found the following code snippet (which was taken from http://wutils.com/wmi/root/cimv2/cim_tachometer/cs-samples.html):
//Project -> Add reference -> System.Management
//using System.Management;
//set the class name and namespace
string NamespacePath = "\\\\.\\ROOT\\cimv2";
string ClassName = "CIM_Tachometer";
//Create ManagementClass
ManagementClass oClass = new ManagementClass(NamespacePath + ":" + ClassName);
//Get all instances of the class and enumerate them
foreach (ManagementObject oObject in oClass.GetInstances())
{
//access a property of the Management object
Console.WriteLine("Accuracy : {0}", oObject["Accuracy"]);
}
And so I tried implementing it in my code:
public static String[] GetFanInfo()
{
ManagementClass cSpeed = new ManagementClass
("\\\\.\\ROOT\\cimv2:CIM_Tachometer"); //Create ManagementClass for the current speed property
ManagementObjectSearcher temp = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature"); //Create management object searcher for the temperature property
ManagementObjectSearcher mos = new ManagementObjectSearcher
("SELECT * FROM Win32_Fan"); //Create a management object searcher for the other properties
string[] Id = new string[8]; //Preparig a string array in which the results will be returned
Id[0] = "Fan"; //First value is the category name
foreach (ManagementObject mo in mos.Get())
{
Id[1] = mo["Name"].ToString(); //Name of the component
Id[2] = mo["Status"].ToString(); //Component's status
long vel = Convert.ToInt64(mo["DesiredSpeed"]); //Desired speed of the component
Id[4] = Convert.ToString(vel);
bool s = Convert.ToBoolean(mo["variableSpeed"]); //Wheater or not variable speed are supported
Id[5] = s.ToString();
break;
}
foreach (ManagementObject obj in temp.Get())
{
Double temperature = Convert.ToDouble(obj["CurrentTemperature"].ToString()); //Fetching the temperature
Id[3] = Convert.ToString((temperature - 2732) / 10.0) + " C";
}
foreach (ManagementObject sObject in cSpeed.GetInstances()) //Get all instances of the class and enumerate them
{
Id[7] = sObject["CurrentReading"].ToString(); //Getting the current reading
}
return Id;
}
To my surprise, it seems that the whole section of the current reading is skipped during runtime. occur anyway!
My question is, why is this certain part skipped? Is the Tachometer a sensor which cannot be used? is it disabled for some reason?
Thanks ahead.
P.S.
I'm writing the program in Microsoft Visual Studio 2015 using winforms for the user interface.