I want to kill a process from list. Because of this I first list the processes and then use process.kill()
. But it doesn't work. Below is the code and I don't know what I'm doing wrong or what I have to do. (I have Windows 7). Can you help?
private void btnProcess_Click(object sender, EventArgs e)
{
UpdateProcessList();
}
private void btnRemove_Click(object sender, EventArgs e)
{
try
{
foreach (Process p in Process.GetProcesses())
{
string strName = listBox1.SelectedItem.ToString();
if (p.ProcessName == strName)
{
p.Kill();
}
listBox1.Items.Remove(strName);
}
UpdateProcessList();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void UpdateProcessList()
{
listBox1.Items.Clear();
foreach (Process p in Process.GetProcesses())
{
listBox1.Items.Add(p.ProcessName);
}
listBox1.Sorted = true;
}