restart my computer

G

Guest

Hi,

is there any easy way to restart my computer... I have XP installed on my
machin.
I have tried this code but it is not working, I am getting an exception
every time it runs.


ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
ManagementBaseObject inParams, outParams;
int result;
W32_OS.Scope.Options.EnablePrivileges = true;

foreach(ManagementObject obj in W32_OS.GetInstances())

{

inParams = obj.GetMethodParameters("Win32Shutdown");
inParams["Flags"] =ShutDown.ForcedReboot;
inParams["Reserved"] = 0;
outParams = obj.InvokeMethod("Win32Shutdown", inParams,null);
result = Convert.ToInt32(outParams["returnValue"]);
}

An unhandled exception of type 'System.Management.ManagementException'
occurred in system.management.dll

Additional information: Privilege not held.


thank u
 
N

Nicholas Paldino [.NET/C# MVP]

Lamis,

You have to have the SE_SHUTDOWN_NAME privledge. If you don't have it,
you can try and get it by calling AdjustTokenPrivileges through the P/Invoke
layer.

Hope this helps.
 
W

Willy Denoyette [MVP]

As a result of a bug in the framework version 1.X, calling this method
results in a Privilege exception.

Willy.

| Hi,
|
| is there any easy way to restart my computer... I have XP installed on my
| machin.
| I have tried this code but it is not working, I am getting an exception
| every time it runs.
|
|
| ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
| ManagementBaseObject inParams, outParams;
| int result;
| W32_OS.Scope.Options.EnablePrivileges = true;
|
| foreach(ManagementObject obj in W32_OS.GetInstances())
|
| {
|
| inParams = obj.GetMethodParameters("Win32Shutdown");
| inParams["Flags"] =ShutDown.ForcedReboot;
| inParams["Reserved"] = 0;
| outParams = obj.InvokeMethod("Win32Shutdown", inParams,null);
| result = Convert.ToInt32(outParams["returnValue"]);
| }
|
| An unhandled exception of type 'System.Management.ManagementException'
| occurred in system.management.dll
|
| Additional information: Privilege not held.
|
|
| thank u
| --
| LZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top