PDA

Click to See Complete Forum and Search --> : [1.0/1.1] Shutdown / Restart computer


JenniferBabe
Jul 31st, 2006, 12:33 PM
Hi guys. I know a way to shutdown and restart a machine but it is not doing exactly what I want. It is bringing up a 'Remote Option Dialog' for the user to choose whether to shutdown or restart the machine or not. What I want is when this code executes, the machine shutdowns instantly without asking the user to choose an option. This is the simple way I know:

Shutdown: System.Diagnostics.Process.Start("Shutdown", "-i");


Anyone knows how to shutdown or restart a computer directly without bringing up this dialog box?


Jennife

jmcilhinney
Jul 31st, 2006, 06:45 PM
What you are doing is running the shutdown.exe program with the "-i" switch. To find out all the other commandline arguments you can pass to shutdown just open a command window and type "shutdown /?" or just "shutdown" and it will display a help page for the program detailing what arguments it can accept.

Harsh Gupta
Jul 31st, 2006, 07:11 PM
try this://add System.Runtime.InteropSrevices
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("Log Off");
comboBox1.Items.Add("Shut Down");
comboBox1.Items.Add("ReBoot");
comboBox1.Items.Add("Forced Log Off");
comboBox1.Items.Add("Forced Shut Down");
comboBox1.Items.Add("Forced ReBoot");
comboBox1.SelectedIndex = 0;
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
switch(comboBox1.SelectedIndex)
{
case 0: ExitWindowsEx(0,0); //LogOff
break;

case 1: ExitWindowsEx(1,0);//Shutdown
break;

case 2: ExitWindowsEx(2,0);//reboot
break;

case 3: ExitWindowsEx(4,0);//forced logoff
break;

case 4: ExitWindowsEx(5,0);//forced shutdown
break;

case 5: ExitWindowsEx(6,0);//forced reboot
break;
}
}

catch(Win32Exception ex)
{}

finally
{
Application.Exit();
}
}there is another way, using System.Management, but i could not use it to perform tasks other than Shutdown.

JenniferBabe
Jul 31st, 2006, 09:12 PM
Hi. I tried your code but something is missing. I put in the header: system.runtime.interopservices but it's still not recognizing the ExitWindowsEx command. Is there another namespace I need for use this function?

Jennifer

jmcilhinney
Jul 31st, 2006, 10:02 PM
ExitWindowsEx is a Windows API function which Harsh Gupta has omitted the declaration for. If you intend to use your app on NT-based versions of Windows only, which includes NT itself, 2000, XP and 2003, then you are safe to use shutdown.exe. If your app will or might be used on earlier versions then ExitWindowsEx will be required. If you want some good code examples and documentation for using ExitWindowsEx then follow the Mentalis link in my signature and check out their WindowsController class.

Harsh Gupta
Aug 1st, 2006, 08:35 AM
:mad: i hate when i miss something in my post. sorry!!

previous post edited!!

JenniferBabe
Aug 1st, 2006, 09:15 PM
jmcilhinney - I"m currently reading the documentation in that link, thanks.

Harsh Gupta - I tried your code and it work but not totally. The logoff is working:

ExitWindowsEx(0,0) and ExitWindowsEx(4,0)

but the others aren't. I tried out a number of different computers, all with either XP or windows server 2003. Do you think you know what is the problem? It's not generating an exception.

Jennifer

Harsh Gupta
Aug 2nd, 2006, 10:31 AM
jmcilhinney - I"m currently reading the documentation in that link, thanks.

Harsh Gupta - I tried your code and it work but not totally. The logoff is working:

ExitWindowsEx(0,0) and ExitWindowsEx(4,0)

but the others aren't. I tried out a number of different computers, all with either XP or windows server 2003. Do you think you know what is the problem? It's not generating an exception.

Jennifer
i am sorry, i couldn't understand why ExitWindowEx API not working. it used to work on VB.

please take a look at these links: code-project (http://www.codeproject.com/csharp/timercomputershutdown.asp), and Link 2 - see post no.2 (http://forums.c-sharpcorner.com/F_ShowMessages.aspx?ThreadID=22968).

otherwsie, you still have the option to start the Shutdown process with additional switches.

penagate
Aug 2nd, 2006, 11:26 AM
InitiateShutdown() (http://msdn.microsoft.com/library/en-us/shutdown/base/initiateshutdown.asp?frame=true).

jmcilhinney
Aug 2nd, 2006, 07:35 PM
Did you read the "Requirements" section on that page penagate?

penagate
Aug 2nd, 2006, 10:29 PM
I can't say I did. Sorry, I meant InitiateSystemShutdown() (http://msdn.microsoft.com/library/en-us/shutdown/base/initiatesystemshutdown.asp?frame=true).

JenniferBabe
Aug 3rd, 2006, 11:24 PM
HI. I got it to work. I used the link that Gupta gave :

http://forums.c-sharpcorner.com/F_ShowMessages.aspx?ThreadID=22968


which used the system.management object.


If anyone wants me to paste my code, please leave a reply.

Jennifer.


P.S. Thanks everyone.

gjon
Sep 6th, 2007, 01:39 PM
Yes, I would love to see your code. I was wanting to perform the same thing.

Thanks in advance.