Results 1 to 13 of 13

Thread: [1.0/1.1] Shutdown / Restart computer

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  4. #4

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [1.0/1.1] Shutdown / Restart computer

    i hate when i miss something in my post. sorry!!

    previous post edited!!
    Show Appreciation. Rate Posts.

  7. #7

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [1.0/1.1] Shutdown / Restart computer


  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Shutdown / Restart computer

    Did you read the "Requirements" section on that page penagate?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [1.0/1.1] Shutdown / Restart computer

    I can't say I did. Sorry, I meant InitiateSystemShutdown().

  12. #12

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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.

  13. #13
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    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
  •  



Click Here to Expand Forum to Full Width