Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Kill all instances of a process running on your machine using System.Diagnostics Namespace in C# .NET 2.0.

Author: Zunnair
Download Source Code : 717_ProcessKillApp.zip

In this simple article you will learn that how you can kill instances of any process running on your system using System.Diagnostics Namespace.

Simply just create New Project of windows application in C#.

 

Import Diagnostics APIs

 

using System.Diagnostics;

 

Drag one label, one buttons and one text box on the form.

 

Now write following code on form Load event

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Process Killing Application";

            label1.Text ="Process Name";

            button1.Text = "Kill";

            textBox1.Text = "NotePad";

        }

 

Now write code on button click event

 

private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text.Length > 0)

 {

   if (RemoveInstances(textBox1.Text))

                    MessageBox.Show("All Instances of " + textBox1.Text + " Removed");

                else

                    MessageBox.Show("All Instances of " + textBox1.Text + " can't Removed");

            }

            else

            {

                MessageBox.Show("Please Enter Valid Process Name...");

     }

 }

 

Now here is the funciton which kill the process

 

public bool RemoveInstances(string AppName)

        {

            bool removed = false;

            try

            {

                AppName = AppName.ToUpper();

                AppName = AppName.Replace(".EXE", "");

                System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();

                foreach (System.Diagnostics.Process proces in prs)

                {

                    if (proces.ProcessName.ToUpper() == AppName)

                    {

                        proces.Refresh();

                        if (!proces.HasExited)

                            proces.Kill();

                    }

                }

                removed = true;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

                removed = false;

            }

            return removed;

        }

 

In this funciton proces.Kill(); command is used for killing the process. Here proces is the object of System.Diagnostics.Process namespace.

Article Comments
This articale is very useful for killing process to task maneger

Posted on 5/6/2008 4:57:34 AM by rupamati

hey,
I want to kill specific EXCEL processes.
Suppose 3 excel processes are running. and only 1 excel file is open.
I want to kill those 2 EXCEL procceses which r no longer use.
I used this code:

Dim prs() As System.Diagnostics.Process
Dim process As System.Diagnostics.Process
prs = System.Diagnostics.Process.GetProcesses()

For Each process In prs

If process.ProcessName.ToUpper() = "EXCEL" Then
process.Refresh()


If Not process.HasExited = True Then
If process.MainWindowHandle = IntPtr.Zero And process.MainWindowTitle.Trim() = "" Then
process.Kill()
End If
End If
End If
Next
obj.Close()
obj = Nothing


It is working fine when i run it from Visual Studio 2005. If i create setup of that application then it will kill all the processes.
I want to kill those processes which are no longer use.
can u suggest me how to do that

Posted on 5/23/2008 4:47:42 AM by Chetan Malegaonkar

I get the following exception when I try to kill my Excel processes that are supposedly no longer running.

System.ApplicationException: Killing Excel Caused this exceptionSystem.ComponentModel.Win32Exception: Access is denied
at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.get_HasExited()
at Axiom.UI.Presenter.NUnitTests.ExcelCamelsProofWork.RemoveExcelInstances() in C:\Documents and Settings\ahall\My Documents\Visual Studio 2008\Projects\Axiom\Axiom.UI.Presenter.NUnitTests\ExcelCamelsProofWork.vb:line 303

Any ideas?

Posted on 6/27/2008 12:03:40 PM by Adron

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get all processes running on machine using System.Diagnostics Namespace in C# .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net