ASP. NET implementation reboot system or shutdown

Source: Internet
Author: User

In the C # program to implement the computer shutdown, restart, two methods can be implemented:

Method 1: Start the shell process and invoke the external command Shutdown.exe to implement.

First import namespaces using System.Diagnostics; Then, in the event where you need to set the shutdown, such as a button click event, within the method of this event:

ProcessStartInfo PS = new ProcessStartInfo ();      Ps. FileName = "Shutdown.exe";         Ps. Arguments = "-s-t 1"; Shutdown, restart the words modified-s for-T          Process.Start (PS);

This will enable the shutdown. Rebooting only needs to be changed to PS. Arguments = "-r-t 1".

Method 2: Call the System API function. The operating system shuts down.

In User32.dll, a system API function called ExitWindowsEx is declared, which can be used to shut down the computer.

function function: This function logs off the current user, shuts down the system, or shuts down and restarts the system. This function sends wm_queryendsession messages to the application to determine whether they can be terminated.

Prototype: BOOL ExitWindowsEx (UINT Uflags,dword dwreserved);

Parameter: uflags; Specifies the shutdown type.

This parameter must include one of the following values: Ewx_logoff,ewx_poweroff,ewx_reboot,ewx_shutdown.

Also includes Ewx_force,ewx_forceifhung two optional values.

Ewx_logoff: Close all processes that are running in the security environment of the process that called the function ExitWindowsEx, and then log off the user. Ewx_reboot: Shut down the system and reboot the system.

Ewx_shutdown: Shut down the system so that it shuts down completely, all the file buffers are purged to disk, and all running processes are stopped.

Because the type processing inside C # is not the same as the prototype definition, the code needs to be changed a bit, but the function is called anyway. Calling the API requires importing namespaces, using System.Runtime.InteropServices; Then, defining the external function under the class, With your field and other functions of the same class, before the definition, but also to import the DLL library file, the same location is under the classes,

[DllImport ("User32.dll")]        public static extern bool ExitWindowsEx (int doflag, int rea);        Internal const int ewx_logoff = 0x00000000;        Internal const int ewx_shutdown = 0x00000001;        Internal const int ewx_reboot = 0x00000002;        Internal const int ewx_force = 0x00000004;        Internal const int ewx_poweroff = 0x00000008;

So even if the definition is complete, the call is as follows: ExitWindowsEx (Ewx_shutdown, 0); This function has a bool return value, and you can choose to receive it.

ASP. NET implementation reboot system or shutdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.