The flag parameter in the win32_operationsystem method win32shutdown (FLAG) in WMI can be any of the following table:
Value description
0 logout
0 + 4 force logout
1 Shutdown
1 + 4 Force Shutdown
2 restart
2 + 4 force restart
8 power off
8 + 4 force power off
The following is an example:
// Close the computer
Private void btn_shutdown_click (Object sender, eventargs E)
{
String ipshutdown = "192.168.1.100 ";
Dialogresult dlresult = MessageBox. Show ("are you sure you want to turn off" + ipshutdown + "" power? "," Please confirm ", messageboxbuttons. yesno, messageboxicon. Question );
If (dlresult = dialogresult. Yes)
{
String [] inparams = {"8", "4 "};
Bootcomputer shutdownbootcomputer = new bootcomputer ();
Shutdownbootcomputer. Strip = ipshutdown;
Shutdownbootcomputer. stradmin = txtadmin. Text. Trim ();
Shutdownbootcomputer. strpassword = txtpassword. Text. Trim ();
Shutdownbootcomputer. strmothod = "win32shutdown ";
Shutdownbootcomputer. inparams = inparams;
Shutdownbootcomputer. bootmachine ();
}
}
// Shut down and restart the computer (multithreading supported)
Public class bootcomputer
{
Public String strip, stradmin, strpassword, strmothod;
Public String [] inparams;
Public void bootmachine ()
{
Connectionoptions bootconn = new connectionoptions ();
Bootconn. Username = stradmin;
Bootconn. Password = strpassword;
Managementscope MS = new managementscope ("\\\\" + strip + "\ Root \ cimv2", bootconn );
Ms. Options. enableprivileges = true;
If (! String. isnullorempty (stradmin )&&! String. isnullorempty (strpassword ))
{
Try {Ms. Connect ();}
Catch {}
}
If (Ms. isconnected)
{
Try
{
Objectquery OQ = new objectquery ("select * From win32_operatingsystem ");
Managementobjectsearcher MOS = new managementobjectsearcher (MS, OQ );
Managementobjectcollection MOC = MOS. Get ();
Foreach (managementobject Mo in MoC)
{
String [] Ss = inparams;
Mo. invokemethod (strmothod, SS );
}
}
Catch (exception ex)
{
MessageBox. Show (strip + ":" + ex. Message + "the network is disconnected or the user name and password are incorrect! ");
}
}
}
}