各位大侠,我现在用WMI编一个程序重启电脑,程序如下:
using System;
using System.Management;namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
ManagementClass DisplayClass = new ManagementClass("Win32_OperatingSystem");
DisplayClass.Scope.Options.EnablePrivileges = true; foreach(ManagementObject mo in DisplayClass.GetInstances())
{
mo.InvokeMethod("Reboot",null,null);
}
}
}
}
但是在编译的时候出现问题,老是说InvokeMethod的参数有问题:
未处理的异常: System.Management.ManagementException: 没有保留特权。
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementObject.InvokeMethod(String methodName, Managem
entBaseObject inParameters, InvokeMethodOptions options)
at ConsoleApplication1.Class1.Main(String[] args) in c:\study\consoleapplicat
ion1\class1.cs:line 25
请大家帮帮我这个菜鸟啊!!!
using System;
using System.Management;namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
ManagementClass DisplayClass = new ManagementClass("Win32_OperatingSystem");
DisplayClass.Scope.Options.EnablePrivileges = true; foreach(ManagementObject mo in DisplayClass.GetInstances())
{
mo.InvokeMethod("Reboot",null,null);
}
}
}
}
但是在编译的时候出现问题,老是说InvokeMethod的参数有问题:
未处理的异常: System.Management.ManagementException: 没有保留特权。
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementObject.InvokeMethod(String methodName, Managem
entBaseObject inParameters, InvokeMethodOptions options)
at ConsoleApplication1.Class1.Main(String[] args) in c:\study\consoleapplicat
ion1\class1.cs:line 25
请大家帮帮我这个菜鸟啊!!!
using System.Management;// This sample demonstrates invoking a WMI method using parameter objects
public class InvokeMethod
{
public static void Main()
{ //Get the object on which the method will be invoked
ManagementClass processClass = new ManagementClass("Win32_Process"); //Get an input parameters object for this method
ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); //Fill in input parameter values
inParams["CommandLine"] = "calc.exe"; //Execute the method
ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null); //Display results
//Note: The return code of the method is provided in the "returnValue" property of the outParams object
Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
}
}
Process.Start("cmd.exe","/c shutdown -r -t 0 -f");
但是Win32_OperatingSystem.Reboot没有参数:(
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
这一句有用吗?singlepine(小山) :
我重新编译了很多遍了:(
http://www.hightechtalks.com/archive/index.php/t-330152-problem-with-reboot-in-c-application.html在里面,Willy Denoyette [MVP]说“due to a bug in .NET v 1.0 SP3 and v1.1 SP1”在下面还提到了使用API的方式,应该对lz有帮助了。
http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx