我正在研究一个应该包括计算机粉丝状态的项目。我需要的大部分属性都可以从Win32_Fan
类中获得。遗憾的是,我找不到使用此类获取当前风扇速度读数的方法。在Win32_Fan MSDN 页面中,“DesiredSpeed”属性中提到当前速度由名为的传感器确定CIM_Tachometer
:
期望速度
数据类型:uint64
访问类型:只读
限定词:单位(“每分钟转数”)
当前请求的风扇速度,以每分钟转数定义,当支持变速风扇时(VariableSpeed 为 TRUE)。当前速度由使用CIM_AssociatedSensor关系与风扇关联的传感器 ( CIM_Tachometer ) 确定。
此属性继承自 CIM_Fan。
有关在脚本中使用 uint64 值的更多信息,请参阅 WMI 中的脚本。
看到之后,我搜索了这个 Tachometer CIM 传感器并找到了以下代码片段(取自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"]);
}
所以我尝试在我的代码中实现它:
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;
}
令我惊讶的是,当前阅读的整个部分似乎在运行时被跳过了。无论如何都会发生!
我的问题是,为什么这一部分被跳过了?转速表是不能使用的传感器吗?它是否由于某种原因被禁用?
提前谢谢。
附言
我正在 Microsoft Visual Studio 2015 中使用 winforms 作为用户界面编写程序。