473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

restart my computer

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
May 10 '06 #1
2 2974
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lamis" <La***@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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

May 10 '06 #2
As a result of a bug in the framework version 1.X, calling this method
results in a Privilege exception.

Willy.

"Lamis" <La***@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
| 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
May 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JonBoy | last post by:
Is it possible to put the logoff/shutdown/restart buttons on an ASP page or something similar? If so can anyone point me in the direction of some code please or where I can read how to do it ...
3
by: nek | last post by:
Greetings, I'm using DB2 WSE V8.1 FP2 on W2K SP4. Right clicking 'properties' from 'DB2 - DB2' service (Admin Tools -> Services), then selecting 'recovery' tab allows me to select computer's...
2
by: John Spiegel | last post by:
Hi all, Is there a way to programmatically restart Windows (2000, specifically), login automatically then start an application? I've been running across some Windows Shell options (which...
3
by: | last post by:
I want use C# to restart my computer, how to?
1
by: Norman Yuan | last post by:
In my cuurent ASP.NET project, a weird thing happens: when I test-run the project and a uncatched run error occurs, VS quits the test-run, as expected. If I try to run the project again (either...
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
8
by: kenneth fleckenstein nielsen | last post by:
hi guru's I want to restart my webservice when ever it throws an exception that isn't cought. can i do that by web.config or iss or how ??
5
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
1
by: =?Utf-8?B?Tk9HT0dMTw==?= | last post by:
When I try to restart my computer it freezes at "Windows is shutting down" screen and I have to manually shut it down. Could someone help me solve this problem. My computer is a HP note book...
3
by: bizcoach | last post by:
I acted too quickly today and now I need help in restarting my computer. I hope this is the right section for posting this. I have a Dell Inspirion laptop with Windows XP. My computer wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.