Home > DeveloperSection > Articles > Check Running Process in CSharp .NET

Check Running Process in CSharp .NET


C# C# 
Ratings:
0 Comment(s)
 4494  View(s)
Rate this:

HOW TO GET PROCESS IN C#.NET

Process Check application shows the currently running processes. You can also end running process.

Form Details

Objects name

Form:frmProcess

Select list box:     lstProcess

Process currently running list box: lstAll

Check button:   btnCheck

End Process button: btnKill

Check All button: btnCheckAll

Exit button: btnExit

Form code with Explanation

 

//Code for btnExit click event.

      private void btnExit_Click(object sender, EventArgs e)

        {

//Close Application

            this.Close();

        }

 

//Code for btnCheck click event. This will check for the selected process in the select list box(lstProcess)

        private void btnCheck_Click(object sender, EventArgs e)

        {

            Boolean found = false; //store the status for record found or not

            string selItem = Convert.ToString(lstProcess.SelectedItem);//storing the selected item from the list box in selItem

            selItem = selItem.ToUpper();

//converting value of selItem toupper case.

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

//assigning running  process in array

// loop to check all running process

            for (int i = 0; i < myProcesses.Length; i++)

            {  

//checking whether selected process is running or not. If running then value of found will be assigned //‘true’            

                if (selItem == myProcesses[i].ProcessName.ToUpper())

                    found = true;

            }

            if (found)

                MessageBox.Show(selItem + " is running !!!");

            else

                MessageBox.Show(selItem + " is not running !!!");

           

        }

 

//code for button btnCheckAll. This will show all the running processes

//in list box ‘lstAll’

        private void btnCheckAll_Click(object sender, EventArgs e)

        {

            lstAll.Items.Clear(); //this will clear the list box.

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

//fetching all running processes and storing in myProcess array.

//loop for adding all running process in list box.

            for (int i = 0; i < myProcesses.Length; i++)

                lstAll.Items.Add(myProcesses[i].ProcessName);

slblProcess.Text = "No. of Processes running: "+myProcesses.Length;      //displaying no. of running processes in status bar.

            btnKill.Enabled = true;

            lstAll.SetSelected(0, true);

        }

 

        private void frmProcess_Load(object sender, EventArgs e)

        {

            this.CenterToScreen();

            lstProcess.SetSelected(0, true);

        }

 

 

//code for btnKill click event.

        private void btnKill_Click(object sender, EventArgs e)

        {           

            string selItem = Convert.ToString(lstAll.SelectedItem);

            string process;           

            selItem = selItem.ToUpper();

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

//loop to check for running process selecter from lstAll list box.

            for (int i = 0; i < myProcesses.Length; i++)

            {

                process = myProcesses[i].ProcessName;

                process = process.ToUpper();

                try

                {

                    if (selItem == process)

                        myProcesses[i].Kill();

//killing process selected from lstAll list box

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

           

        }

   

Screen shots


Don't want to miss updates? Please click the below button!

Follow MindStick