[1.0/1.1] Shutdown / Restart computer
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
Re: [1.0/1.1] Shutdown / Restart computer
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.
Re: [1.0/1.1] Shutdown / Restart computer
try this:
Code:
//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.
Re: [1.0/1.1] Shutdown / Restart computer
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
Re: [1.0/1.1] Shutdown / Restart computer
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.
Re: [1.0/1.1] Shutdown / Restart computer
:mad: i hate when i miss something in my post. sorry!!
previous post edited!!
Re: [1.0/1.1] Shutdown / Restart computer
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
Re: [1.0/1.1] Shutdown / Restart computer
Quote:
Originally Posted by JenniferBabe
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, and Link 2 - see post no.2.
otherwsie, you still have the option to start the Shutdown process with additional switches.
Re: [1.0/1.1] Shutdown / Restart computer
Re: [1.0/1.1] Shutdown / Restart computer
Did you read the "Requirements" section on that page penagate?
Re: [1.0/1.1] Shutdown / Restart computer
I can't say I did. Sorry, I meant InitiateSystemShutdown().
Re: [1.0/1.1] Shutdown / Restart computer
HI. I got it to work. I used the link that Gupta gave :
http://forums.c-sharpcorner.com/F_Sh...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.
Re: [1.0/1.1] Shutdown / Restart computer
Yes, I would love to see your code. I was wanting to perform the same thing.
Thanks in advance.