0

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;
        }
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
samuraisxmali
  • 103
  • 3
  • 11
  • Do you have privileges to kill that process? – Adriano Repetti Mar 06 '12 at 09:34
  • I'm admin on this computer and running it with admin privileges... – samuraisxmali Mar 06 '12 at 09:36
  • What process are you trying to kill? Does your code work to kill an instance of Notepad.exe? – Dan Puzey Mar 06 '12 at 09:38
  • 1
    What does "doesn't work" mean? You should at least tell us what's going wrong (exception thrown...etc.). – ken2k Mar 06 '12 at 09:38
  • Make sure you *really* are an admin. On Windows 7 and Vista if User Access Control is enabled administrative accounts are not really administrators. So you need to be either running under *the* Administrator account or turn UAC off. – Rick Strahl Mar 06 '12 at 09:38
  • don't work means that it doesn't kills process. I have seen the other question about making privileges to kill process, but how to use it in my project can anyone tell me? I mean this: http://stackoverflow.com/questions/544687/programmatically-kill-a-process-in-vista-windows-7-in-c-sharp – samuraisxmali Mar 06 '12 at 09:47

3 Answers3

4
            foreach (Process p in Process.GetProcesses())
            {
                string strName = listBox1.SelectedItem.ToString();

                if (p.ProcessName == strName)
                {
                    p.Kill();
                }
                listBox1.Items.Remove(strName);
            }

There's a logical error in your code. You call the Remove() method even if the process name does not match. This code can only work if the selected item is the first one returned by GetProcesses(), very low odds for that. The far more common outcome is that you'll remove the item from the list on the very first pass through the loop and end up killing nothing. Easy to see with a debugger.

A simple workaround is to move the Remove() call inside the if() statement block.

An entirely better approach is:

        foreach (var p in Process.GetProcessesByName(listBox1.SelectedItem.ToString()) {
            p.Kill();
        }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

It is because you do not have the admin previlages.

Follow the below post

programmatically kill a process in vista/windows 7 in C#

Community
  • 1
  • 1
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
0

In order to kill a process you have to run under an administrative account. This means either that you are a 'true' administrator or you User Account Control (UAC) turned off.

Otherwise Process.Kill() will fail.

Rick Strahl
  • 17,302
  • 14
  • 89
  • 134