Get the current process of the system and add it to ListBox:
System. Diagnostics. Process [] processoncomputer = system. Diagnostics. process. getprocesses ();
Foreach (system. Diagnostics. Process P in processoncomputer)
{
This. listbox1.items. Add (P. processname );
}
Shut down a specified process:
System. Diagnostics. Process [] process = system. Diagnostics. process. getprocessesbyname (this. listbox1.selecteditem. tostring ());
Foreach (system. Diagnostics. Process P in process)
{
P. Kill ();
}
However, this will close all instances of the process. For example, if you open multiple ie instances, all IE Windows will be closed.
The following code disables a specific ie instance:
Declare An arraylist first:
Arraylist tables whandle = new arraylist ();
Get all the instances of the specified process, put them in a ListBox, and put the handle of the main window into arraylist:
System. Diagnostics. Process [] processoncomputer = system. Diagnostics. process. getprocessesbyname (this. listbox1.selecteditem. tostring ());
Foreach (system. Diagnostics. Process P in processoncomputer)
{
This. listbox2.items. Add (P. mainwindowtitle); // display the title of the main form in ListBox
Define whandle. Add (P. main1_whandle );
}
Compares the handle in the main window of the specified process with that in arraylist. If yes, It is disabled.
System. Diagnostics. Process [] process = system. Diagnostics. process. getprocessesbyname (this. listbox1.selecteditem. tostring ());
Foreach (system. Diagnostics. Process P in process)
{
If (P. main1_whandle = (system. intptr) (when whandle [This. listbox2.selectedindex])
{
P. Kill ();
This. listbox2.items. removeat (this. listbox2.selectedindex );
}
}
In this way, the process with the main form can be closed, but the process without the main form does not work.