Introduction
This article provides a simple example of how to use the System.Diagnostics.Process library to display a list of running processes, and to select and kill processes by their process name and ID. It would not be a major trick to display additional information about the process using the same library or to kill processes by their ID or name alone rather than their name and ID but in this example, process names and IDs are used for display purposes and the purpose of killing a running process.
Figure 1: Listing and Killing Processes by Process Name and ID
Getting Started
There is a single solution included with this download, the solution contains a Win Forms project called "ProcessKiller"; this project contains one form (Form1.cs) and all of the code required to display and kill processes is contained within that single form class. If you open the attached project into Visual Studio 2008; you should see the following in the solution explorer:
Figure 2: Solution Explorer
Code: The Main Form
The main form contains all of the user interface elements necessary to display and update the list of currently running processes found on the user's machine. It also contains a button handler which will kill a process by the process name. You can also kill a process by its ID but in terms of readability, if the user is selecting the process, the process name is going to make a lot more sense to them than will the process ID.
The class begins with the normal and default imports:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
The constructor calls a method called "UpdateProcessList" which is used to get and display a list of running processes; it will be described shortly.
namespace ProcessKiller
{
public partial class frmProcessKiller : Form
{
/// <summary>
/// constructor
/// </summary>
public frmProcessKiller()
{
InitializeComponent();
// get an initial list of processes
UpdateProcessList();
}
The next bit of code is the UpdateProcessList function; this function clears the listbox of any existing entries and then loops through each of the current processes, adding the name and ID of each process to the list (as Process Name – ID). At the end of the function, a status message is updated to display the current number of found processes.
/// <summary>
/// Loop through the list of running processes
/// and add each process name to the process
/// listbox
/// </summary>
private void UpdateProcessList()
{
// clear the existing list of any items
lstProcesses.Items.Clear();
// loop through the running processes and add
//each to the list

Varsha VenugopalPosted May 16, 2014, 6:35 AM
How can i differentiate the important system processes??? I need to run only my application processes and system processes, other processes should be killed or should not be allowed to run when my application is running.
Joe BaePosted Mar 25, 2010, 1:35 PM
The code provided above was a great help for me to figure out how to kill multiple instances of a specific process. Below is my code with MSWORD as example. Thanks. [code] private void KillProcess() { Boolean isAlive = true; while (isAlive == true) { foreach (Process runningProcess in System.Diagnostics.Process.GetProcesses()) { if (runningProcess.ProcessName == "MSWORD") { runningProcess.Kill(); } else { isAlive = false; } } } } [/code]
ismail SARIKAYAPosted Nov 8, 2008, 8:44 AM
When i tryd to kill a process, i am getting an error "Access Denied"