如何设置箱子风扇速度

我想创build代码设置机箱风扇的速度,并必须在Windows 7上工作。

我尝试使用WMI代码创build器,但我得到错误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); } } } } 

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

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

SetSpeed未执行。

既然如此,这条线就会失败:

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

如果没有实现 SetSpeed (而不是简单的忽略),你会得到一个异常,试图检索与它有关的参数。 删除Try / Catch来验证发生在哪一行。

制造商可能会有一个允许这样做的工具,但WMI似乎有疑问。 如果你确实找到了这样的工具,你可能想要评估bool属性VariableSpeed来查看是否支持变速。