|
-
Jul 31st, 2006, 12:33 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jul 31st, 2006, 06:45 PM
#2
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.
-
Jul 31st, 2006, 07:11 PM
#3
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.
Last edited by Harsh Gupta; Aug 1st, 2006 at 08:34 AM.
-
Jul 31st, 2006, 09:12 PM
#4
Thread Starter
Hyperactive Member
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
-
Jul 31st, 2006, 10:02 PM
#5
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.
-
Aug 1st, 2006, 08:35 AM
#6
Re: [1.0/1.1] Shutdown / Restart computer
i hate when i miss something in my post. sorry!!
previous post edited!!
-
Aug 1st, 2006, 09:15 PM
#7
Thread Starter
Hyperactive Member
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
-
Aug 2nd, 2006, 10:31 AM
#8
Re: [1.0/1.1] Shutdown / Restart computer
 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.
-
Aug 2nd, 2006, 11:26 AM
#9
Re: [1.0/1.1] Shutdown / Restart computer
-
Aug 2nd, 2006, 07:35 PM
#10
Re: [1.0/1.1] Shutdown / Restart computer
Did you read the "Requirements" section on that page penagate?
-
Aug 2nd, 2006, 10:29 PM
#11
Re: [1.0/1.1] Shutdown / Restart computer
I can't say I did. Sorry, I meant InitiateSystemShutdown().
-
Aug 3rd, 2006, 11:24 PM
#12
Thread Starter
Hyperactive Member
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.
-
Sep 6th, 2007, 01:39 PM
#13
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|