【问题标题】:Im using Win32_Fan to get and display my Cpu Fan Speed but it's not working why?我使用 Win32_Fan 来获取和显示我的 Cpu 风扇速度,但它不起作用,为什么?
【发布时间】:2013-03-12 12:40:20
【问题描述】:

这是我在 Form1 构造函数中调用方法的代码:

private void cpuFanSpeed()
        {
            SelectQuery query =
           new SelectQuery("Win32_Fan");

            // Instantiate an object searcher
            // with this query
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(query);

            // Call Get() to retrieve the collection
            // of objects and loop through it
            foreach (ManagementObject envVar in searcher.Get())
                MessageBox.Show(envVar["DesiredSpeed"].ToString());
        }

但它永远不会到达 MessageBox。 这里有什么问题?我试图通过这里的文档阅读和执行:http://msdn.microsoft.com/en-us/library/aa394146(v=vs.85).aspx

在这里:http://msdn.microsoft.com/en-us/library/ms257359.aspx

但它不起作用。

我想在标签上显示我的 CPU 风扇每秒的速度。

这是 OpenHardwareMonitor 显示我的 cpu 风扇速度的屏幕截图:

这是我在我的应用程序中使用的函数获取 CPU 温度的代码:

在课堂上:

public static float? cpuView(bool pause , CpuTemperature cpuTemp , Form1 f1 , List<string> myData , float? myCpuTemp , Button b1)
        {
            if (pause == true)
            {
            }
            else
            {
                Computer myComputer = new Computer();
                myComputer = new Computer(cpuTemp)
                {
                    CPUEnabled =

                        true
                };

                myComputer.Open();
                Trace.WriteLine("");
                foreach (var hardwareItem in myComputer.Hardware)
                {
                    if (hardwareItem.HardwareType == HardwareType.CPU)
                    {
                        hardwareItem.Update();
                        foreach (IHardware subHardware in hardwareItem.SubHardware)
                            subHardware.Update();

                        foreach (var sensor in hardwareItem.Sensors)
                        {
                            cpuTemp.SetValue("sensor", sensor.Value.ToString());
                            if (sensor.SensorType == SensorType.Temperature)
                            {
                                sensor.Hardware.Update();
                                cpuTemp.GetValue("sensor", sensor.Value.ToString());                                
                                f1.Invoke(new Action(() => myData.Add("Cpu Temeprature --- " + sensor.Value.ToString())));
                                myCpuTemp = sensor.Value;
                                if (sensor.Value > 60)
                                {


                                    Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
                                    b1.Enabled = true;
                                }

                                break;
                            }
                        }
                    }
                }
            }
            return myCpuTemp;
        }

【问题讨论】:

  • 您是否遇到任何具体错误?
  • Arshad 不,我只是不知道如何通过打开硬件监视器来获得 cpu 风扇速度

标签: c# .net wmi


【解决方案1】:

并非每台机器都通过 WMI 提供此信息。如果您的计算机没有,您将无法访问它。仅仅因为 WMI 提供了访问特定信息的属性并不意味着该信息将始终可用。

大概,您在foreach 循环中迭代的集合是,这就是为什么没有显示MessageBox

解决此问题的唯一可能方法是从您的主板制造商处获取更新的驱动程序,该驱动程序向 WMI 提供此信息(当然,假设您的硬件甚至包括首先测量此类事物所需的传感器地点)。

编辑: Open Hardware Monitor 显然已经编写了自己的驱动程序来直接与您的硬件交互,查询其传感器。这种怀疑通过仔细阅读他们的网页得到证实,documents specific pieces of hardware that it supports

它没有使用WMI获取它的信息,所以这并不能证明你可以自己从WMI获取信息。

但是,上面链接页面的底部确实包含这个有趣的评论:

Open Hardware Monitor 将所有传感器数据发布到 WMI(Windows Management Instrumentation)。这也允许其他应用程序读取和使用传感器信息。接口的初步文档可以在here找到。

因此,您似乎可以在 Open Hardware Monitor 之上搭载,使用其驱动程序来检索信息,然后在您的应用程序内部从中检索该信息。这可能是最好的解决方案,因为我怀疑您的硬件制造商会通过更新的驱动程序来为 W​​MI 提供风扇速度。

【讨论】:

  • Cody 我几分钟前刚跑过 OpenHardwareMonitor,我可以看到我的 cpu 风扇速度。我还在我的应用程序中使用了 openhardwaremonitor 源代码来获取并显示我的 cpu 和显卡温度没有问题。但是我在 openhardwaremonitor 源中找不到如何获取和显示我的 cpu 风扇速度。我正在更新我的问题。
猜你喜欢
  • 1970-01-01
  • 2013-10-02
  • 2013-06-14
  • 2013-12-27
  • 2011-01-14
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多