为什么这样做:
using System;
using System.Management;
public class InvokeMethod
{
public static void Main()
{
ManagementClass processClass = new ManagementClass("Win32_Process");
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "calc.exe";
InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, methodOptions);
}
}
这不起作用:
using System;
using System.Management;
public class Program
{
public static void Main()
{
ManagementClass rename = new ManagementClass("Win32_ComputerSystem");
ManagementBaseObject inParams = rename.GetMethodParameters("Rename");
inParams.SetPropertyValue("Name", "Name1234");
InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementBaseObject outParams = rename.InvokeMethod("Rename", inParams, methodOptions);
Console.Read();
}
}
这里的问题是这一行:
ManagementBaseObject outParams = rename.InvokeMethod("Rename", inParams, methodOptions);
无效的方法参数。 我知道你必须以管理员的身份运行它,因为它可以做任何事情,但我只是想让它起作用。