如何设置机箱风扇速度

时间:2015-02-02 13:53:31

标签: c# .net windows wmi

我想创建代码,在机箱风扇上设置速度,并且必须在Windows 7上运行。

我尝试使用WMI代码创建器,但我收到错误Invalid object path

    using System;
    using System.Management;
    using System.Windows.Forms;

    namespace WMISample
    {
        public class CallWMIMethod
        {
            public static void Main()
            {
                try
                {
                    ManagementObject classInstance = 
                        new ManagementObject("root\\CIMV2", 
                        "Win32_Fan.ReplaceKeyPropery='ReplaceKeyPropertyValue'",
                        null);

                    // Obtain in-parameters for the method
                    ManagementBaseObject inParams = 
                        classInstance.GetMethodParameters("SetSpeed");

                    // Add the input parameters.
                    inParams["DesiredSpeed"] =  600;

                    // Execute the method and obtain the return values.
                    ManagementBaseObject outParams = 
                        classInstance.InvokeMethod("SetSpeed", inParams, null);

                    // List outParams
                    Console.WriteLine("Out parameters:");
                    Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
                }
                catch(ManagementException err)
                {
                    MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
                }
            }
        }
    }

有没有可能参考案例粉丝。任何帮助都可以得到赞赏?

1 个答案:

答案 0 :(得分:2)

您的代码编译因为它是有效的代码。它在运行时失败,因为你要求它做一些非法的事情。根据{{​​3}}:

  

SetSpeed 未实施。

在这种情况下,这一行将失败:

    ManagementBaseObject inParams = 
        classInstance.GetMethodParameters("SetSpeed");

如果SetSpeed 未实现(而不是简单地忽略),则会尝试检索与其相关的参数。删除Try / Catch以验证它发生在哪一行。

制造商可能有一个允许这样做的实用程序,但看起来WMI会起作用。如果找到这样的工具,您可能需要评估bool属性VariableSpeed以查看是否支持变量速度。