我正在从事一个项目,该项目应该包括计算机的风扇状态。我需要的大多数属性都可以从Win32_Fan
类中获取。可悲的是,我找不到使用此类的方法来获取风扇速度的当前读数。在Win32_Fan MSDN page中,“ DesiredSpeed”属性中提到当前速度由名为CIM_Tachometer
的传感器确定:
所需速度
数据类型: uint64
访问类型:只读
限定词:单位(“每分钟转数”)
当前请求的风扇速度,以每分钟转数定义,当 支持变速风扇(VariableSpeed为TRUE)。目前 速度由关联的传感器( CIM_Tachometer )确定 与风扇之间使用 CIM_AssociatedSensor 关系。
此属性是从CIM_Fan继承的。
有关在脚本中使用uint64值的更多信息,请参见 WMI中的脚本。
看到之后,我搜索了该转速表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;
}
令我惊讶的是,当前读数的整个部分似乎在运行时被跳过了。反正发生!
我的问题是,为什么这部分被跳过了?转速表是不能使用的传感器吗?是由于某种原因禁用的吗?
谢谢。
P.S。
我正在使用winforms作为用户界面在Microsoft Visual Studio 2015中编写程序。