-1

Possible Duplicate:
How to shutdown the computer from C#

I want to bring up a restart computer message box after an event occurs in my Win Form application. What command can I use to restart the computer if the user chooses Yes?

Community
  • 1
  • 1
naren.katneni
  • 275
  • 1
  • 4
  • 10
  • 1
    You may take a look at also http://stackoverflow.com/questions/102567/how-to-shutdown-the-computer-from-c-sharp – Dan Hunex Jan 25 '13 at 00:08

4 Answers4

3

You can p/invoke ExitWindowsEx.

As noted, you'll need to call AdjustTokenPrivileges as well, since SE_SHUTDOWN_NAME is inactive by default.

A whole bunch of information available on MSDN here

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

you should use WINAPI . the below function can do power task for you :

BOOL WINAPI ExitWindowsEx(
  _In_  UINT uFlags,
  _In_  DWORD dwReason
);

Note that this function is in user32.dll. Simply to restart :

ExitWindowsEx(2,4);

Here is the full list of flags : link to msdn

Now here is sample C# Code for this :

using System.Runtime.InteropServices;

[DllImport("user32.dll", SetLastError=true)]

public static extern bool  ExitWindowsEx(uint uFlags,uint dWReason);


public static void Main()
{
  ExitWindowsEx(2,4);
}
S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58
  • ExitWindowsEx(2,4)? Magic number violation detected. Also, what is long int? – David Heffernan Jan 25 '13 at 00:16
  • 4 is for EWX_FORCE and 2 is for EWX_REBOOT . david , plz correct me if there is any problem.. – S.A.Parkhid Jan 25 '13 at 00:18
  • Second paramter is DWORD which is uint in C#. Your parameters should have real names. You need SetLastError = true in the DllImport attribute. And you must stop using magic numbers. Declare consts or enums. – David Heffernan Jan 25 '13 at 00:21
  • thanks for that , I had a mistake in variable types mapping , and SetLastError has been updated... But I had not with you to not to use magic numbers , because here we want to do a single job and they will be addressed immediately by compiler . it is not an unsafe code.I don't think enum will ran faster , but still for keeping the code managed , yes you are true. we should use enum or consts. – S.A.Parkhid Jan 25 '13 at 00:24
  • Speed is not important here. Have you any idea how many clock cycles will be consumed shutting that machine down?! What counts is readability. Write code that a human can read. Don't make the reader trek all the way to MSDN in order to understand your code. – David Heffernan Jan 25 '13 at 00:30
  • in contrast , speed is important every where . you have dived too deeply, clock cycles , i think carbon di oxides that will be generated and the environment's pollution are your next hits. also human can use comments for that code . – S.A.Parkhid Jan 25 '13 at 00:38
  • So you think that using magic constants means faster code? – David Heffernan Jan 25 '13 at 07:11
0
Process.Start("shutdown","/r /t 0");
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
-1

This particular solution worked for me: https://stackoverflow.com/a/102583/1505128

using System.Management;

void Shutdown()
{
    ManagementBaseObject mboShutdown = null;
    ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
    mcWin32.Get();

    // You can't shutdown without security privileges
    mcWin32.Scope.Options.EnablePrivileges = true;
    ManagementBaseObject mboShutdownParams =
             mcWin32.GetMethodParameters("Win32Shutdown");

    // Flag 1 means we want to shut down the system. Use "2" to reboot.
    mboShutdownParams["Flags"] = "1";
    mboShutdownParams["Reserved"] = "0";
    foreach (ManagementObject manObj in mcWin32.GetInstances())
    {
        mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
                                       mboShutdownParams, null);
    }
}
Community
  • 1
  • 1
naren.katneni
  • 275
  • 1
  • 4
  • 10