Program description memo:
The entire program is to remotely start a process, then query the process list, and display it, And then remotely terminate the process selected in the list.
Private managementscope MS = NULL;
Private managementobjectcollection collection = NULL;
Private arraylist processlist = new arraylist ();
If (null = MS)
{
Connectionoptions connop = new connectionoptions ();
Connop. Username = "192.168.168.xx // username ";
Connop. Password = "password ";
Managementpath remotenamespace = new managementpath ("// 192.168.168.xx // root // cimv2 ");
MS = new managementscope (remotenamespace, connop );
}
Try
{
// Connect to the WMI range of the actual operation
Ms. Connect ();
// Set the content to be queried Through WMI
Objectquery query = new objectquery ("select * From win32_process ");
// WQL statement, set WMI query content and WMI operation range, and retrieve the WMI object set
Managementobjectsearcher searcher = new managementobjectsearcher (MS, query );
// Call WMI query Asynchronously
Collection = searcher. Get ();
Listbox1.items. Clear ();
Processlist. Clear ();
Foreach (managementobject service in Collection)
{
// Because the test does not list all the processes, only the notepad.exe process is displayed.
If (Service ["name"]. Equals ("test.exe "))
{
Processlist. Add (service );
Listbox1.items. Add ("service:" + service ["name"] + "process ID:" + service ["processid"]);
Listbox1.forecolor = color. blue;
}
}
}
Catch (exception ee)
{
MessageBox. Show ("connection" + "ASD" + "error, error message:" + ee. message, "error! ");
}
Try
{
Ms. Connect ();
// Create a remote process operation object
Managementclass processclass = new managementclass (MS, new managementpath ("win32_process"), null );
// Obtain the object used to provide parameters
Managementbaseobject inparams = processclass. getmethodparameters ("CREATE ");
// Set command line parameters
Inparams ["CommandLine"] = "D: // XX // app.exe ";
// Execute the program
Managementbaseobject outparams = processclass. invokemethod ("CREATE", inparams, null );
// Refresh the list
Btnlist_click (null, null );
}
Catch (exception ee)
{
MessageBox. Show ("connection" + "ASD" + "error, error message:" + ee. message, "error! ");
}
If (processlist. Count> 0) & (listbox1.items. Count> 0) & (listbox1.selectedindex>-1 ))
{
Try
{
// Stop a process
(Managementobject) processlist [listbox1.selectedindex]). invokemethod (New managementoperationobserver (), "Terminate", null );
// Refresh the list
// Processlist. removeat (listbox1.selectedindex );
// Listbox1.items. removeat (listbox1.selectedindex );
}
Catch (exception ee)
{
MessageBox. Show ("connection" + "ASD" + "error, error message:" + ee. message, "error! ");
}
}