Ben
Program Only three buttons are used: restart, shutdown, and exit.
Open Solution Explorer on the right, expand tree structure, reference, and add reference. In the displayed dialog box, locate system. management --- double-click to add it --- OK, that is, the system is built. management. DLL reference. Then write at the beginning of the program
Using system. Management;
Using system. runtime. interopservices; // this parameter is added to reference Windows API for shutdown.
The procedure is as follows:
-------------------
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Management;
Using system. runtime. interopservices; // this parameter is added to reference Windows API for shutdown.
Namespace rebootcomputer
{
Public class form1: system. Windows. Forms. Form
{
// --------------- This part is the control --------
Private system. Windows. Forms. Button reboot;
Private system. Windows. Forms. Button shutdown;
Private system. componentmodel. Container components = NULL;
Private system. Windows. Forms. Button exit;
//---------------------------------------------------------
Private managementobjectsearcher OBJ; // for this class, please see the last simple single get system resource information (sharing, hard disk, Nic, etc.) http://itbbs.pconline.com.cn/traditional/article.jsp? Topic = 1540826
// ----------------- Program initialization ----------------
Public form1 ()
{
Initializecomponent ();
}
// ----------------------- Program release ----------------------
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
// ------------------------- Initialize ----------------------
Private void initializecomponent ()
{
This. Reboot = new system. Windows. Forms. Button ();
This. Shutdown = new system. Windows. Forms. Button ();
This. Exit = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Reboot
//
This. Reboot. Location = new system. Drawing. Point (16, 16 );
This. Reboot. Name = "reboot ";
This. Reboot. tabindex = 0;
This. Reboot. Text = "restart ";
This. Reboot. Click + = new system. eventhandler (this. reboot_click );
//
// Shutdown
//
This. shutdown. Location = new system. Drawing. Point (112, 16 );
This. shutdown. Name = "shutdown ";
This. shutdown. tabindex = 1;
This. shutdown. Text = "shutdown ";
This. shutdown. Click + = new system. eventhandler (this. shutdown_click );
//
// Exit
//
This. Exit. Location = new system. Drawing. Point (216, 16 );
This. Exit. Name = "exit ";
This. Exit. tabindex = 2;
This. Exit. Text = "quit ";
This. Exit. Click + = new system. eventhandler (this. exit_click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (304, 53 );
This. Controls. Add (this. Exit );
This. Controls. Add (this. Shutdown );
This. Controls. Add (this. Reboot );
This. Name = "form1 ";
This. Text = "restart, shut down ";
This. resumelayout (false );
}
// ----------------- Main function, which cannot run without it ----------
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
// -------------------- Restart some programs, mainly using the last simple command to obtain system resource information (sharing, hard disk, Nic, etc )----------------
Private void reboot_click (Object sender, system. eventargs E)
{
OBJ = new managementobjectsearcher ("select * From win32_operatingsystem ");
Foreach (managementobject OC in OBJ. Get ())
{
Dialogresult result = MessageBox. Show ("are you sure want to reboot? "," Confirm ", messageboxbuttons. okcancel, messageboxicon. Information); // click" OK "to determine whether the user has clicked the" OK "button.
If (dialogresult. OK = Result)
{
OC. invokemethod ("reboot", null); // click "OK" and call this method to restart the machine
}
}
}
// --------------- Shut down some programs ------------------------
Private void shutdown_click (Object sender, system. eventargs E)
{
If (dialogresult. OK = MessageBox. Show ("are you sure want to shut down? "," Confirm ", messageboxbuttons. okcancel, messageboxicon. Information) // same as above
{// Mainly calls the system API for implementation
Exitwindowsex (1, 0 );
}
}
// Here [dllimport ("user32.dll")] is an expression of the properties in. net. dllimport is the library that references the system API "user32.dll"
[Dllimport ("user32.dll")]
// This is the main API. Note: It must be declared as static extern.
Private Static extern int exitwindowsex (int x, int y );
// This is the implementation of the exit button, that is, the exit of the Program
private void exit_click (Object sender, system. eventargs e)
{< br> application. exit ();
}< BR >}< br> -----------------