Process.Kill() Access Denied

Ezzy picture Ezzy · Aug 8, 2013 · Viewed 31.9k times · Source

When I run the following code, a Win32Exception is thrown for Access Denied. I cannot find any solutions via search. How do I fix this?

foreach (ListViewItem list in showprocesses.SelectedItems)
{
    Process p = System.Diagnostics.Process.GetProcessById(Convert.ToInt32(list.Tag));
    if (p != null)
        p.Kill();
}

Answer

Jason picture Jason · Aug 8, 2013

You will generally get this error if you do not have the necessary permissions. You must be an administrator, and in win vista and above, run your app/process in elevated mode. Furthermore, there are certain processes that even as admin you won't be able to kill, some deemed system critical, etc, and you may need to run as system, and then there are those that even system can't kill, like antivirus, or an actual virus, because they don't want you killing their process

Another possibility is that if the process is already terminating, it will also throw that exception, see MSDN