C # Start and terminate processes remotely

Source: Internet
Author: User

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! ");
}
}

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.