C# 如何设置机箱风扇速度

C# 如何设置机箱风扇速度,c#,.net,windows,wmi,C#,.net,Windows,Wmi,我想创建代码,设置机箱风扇的速度,必须在windows 7上工作 我尝试使用WMI代码创建器,但出现错误无效对象路径 using System; using System.Management; using System.Windows.Forms; namespace WMISample { public class CallWMIMethod { public static void Main(

我想创建代码,设置机箱风扇的速度,必须在windows 7上工作

我尝试使用WMI代码创建器,但出现错误
无效对象路径

    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);
                }
            }
        }
    }

是否有可能参考案例风扇。有什么可以帮忙的吗

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

设置速度未执行

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

    ManagementBaseObject inParams = 
        classInstance.GetMethodParameters("SetSpeed");
如果未实现
SetSpeed
(而不是简单地忽略),您将在尝试检索与其相关的参数时遇到异常。移除Try/Catch以验证它发生在哪一行


制造商可能有一个实用程序可以实现这一点,但WMI是否能工作似乎值得怀疑。如果您确实找到了这样一个工具,您可能需要评估bool属性
VariableSpeed
,以查看是否支持可变速度。

您不是在一小时前问过这个问题吗?根据MSDN,
SetSpeed
不是为
Win32_Fan
实现的,因此我假设您在
classInstance.GetMethodParameters(“SetSpeed”)处得到错误因此,哪一行生成错误?程序编译后,此错误会在运行后显示系统制造商可能有一个实用程序允许此操作,但您应该首先检查风扇是否支持变速(检查
VariableSpeed
bool属性)