I want to kill the multiple processes but also want to keep some specific ones. Actually I have two list box one has the list of processes that I want to kill. The other one has the list which I want to keep.
For example, I want to kill Google Chrome and keep the Calculator on. But the problem with this code is, it's closing all processes even the winform itself.
private void BtnStop_Click(object sender, EventArgs e)
{
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (!String.IsNullOrEmpty(p.MainWindowTitle))
{
//ChBlockApp is the name of the list box that i want to kill
ChBlockApp.Items.Add(p.MainWindowTitle);
try
{
// ListAllowed is the name of listbox which i want to keep
if (p.MainWindowTitle != "Avid-Main Page" | p.MainWindowTitle != ListAllowed.items.toString())
{
p.Kill();
}
}
catch (Exception KillExc)
{
MessageBox.Show("Unable to Close the Applicaiton: " + KillExc, "Warning", MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
}
}
I have tried using a check to keep the visual studio open in this statement.
if (p.MainWindowTitle != "Avid-Main Page")
{}
But it didn't work either.
This Link has some hints but i have multiple programs to keep.