Hey there, so basicly I'm writing a little application that will kill some processes and some junk programs running in the background. Yea i know this can be done in task manager aswell, but I want to learn ^^.

So, I want a button called "Get all running proccesses", when u click that button all processes should get added to a dropdown box, so the user can select the process in the dropdown menu. right now i got this command:

foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName(textBox1.Text))
            {
                try
                {
                    p.Kill();
                    p.WaitForExit();
                    progressBar1.Value = 100;
                    label1.ForeColor = System.Drawing.Color.Green;
                    label1.Text = "Process Successfully Killed";
                    timer1.Start();
                    textBox1.ResetText();
                }
                catch (Win32Exception)
                {

                }
                catch (InvalidOperationException)
                {

                }

it will kill the text entered in the textbox, so i just want to replace the text box with a dropdown menu :P, any thoughts on how i could do this?

Edit: also, is there any way i can make label1 it say "couldnt find process" or something similar if it could find the process? :)

Kind Regards
-Jazerix

Hi there,

Ok, I suggest you to put the list into a ListBox, as it's much readable in this case, if you insist to the ComboBox, I can tell you that version too.

First of all you can add the Diagnostics namespace at the first lines:

using System.Diagnostics;

This is how you get the list and put it in a listbox:

Process[] processes = Process.GetProcesses();
            foreach ( Process process in processes )
                listBox1.Items.Add(process.ProcessName);

As you have the list, you can select an item and press the Kill button. The killbutton eventhandler should be something like this:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                processes[ listBox1.SelectedIndex ].Kill();
                label1.Text = processes[ listBox1.SelectedIndex ].ProcessName + " Killed";
            } catch ( Exception ex )
            {                
                MessageBox.Show(ex.Message);
            }
        }

If there is any trouble like you can't access a process or you didn't select any process, then a messagebox will appear showing the error :)

That's it, if you don't understand any part just ask, good luck!

EDIT: If you want to use a Combobox, then just replace listBox1 with comboBox1, it should work :)
If this solves your problem, please mark this thread as Solved!

commented: Thank you :D +1

thank you so much :)
though there seems to be a problem with the button

processes[listBox1.SelectedIndex].Kill();
                    label1.Text = processes[listBox1.SelectedIndex].ProcessName + " Killed";

it says: "Error 3 The name 'processes' does not exist in the current context C:\Users\Jazerix\AppData\Local\Temporary Projects\Killer\Form1.cs 33 17"

but thanks, the logic seems to be just fine ^^

ok the problem is that you cannot reach processes in the scope you are using it, in english you can declare the processes in the Form like this:

public class Form1 : Form
{
  Process[] processes = Process.GetProcesses();
  
  public Form1()
  {
     InitializeComponents();
     foreach ( Process process in processes )
        listBox1.Items.Add(process.ProcessName);
  }

  private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                processes[ listBox1.SelectedIndex ].Kill();
                label1.Text = processes[ comboBox1.SelectedIndex ].ProcessName + " Killed";
            } catch ( Exception ex )
            {                
                MessageBox.Show(ex.Message);
            }
        }
}

so this way processes can be accessed in the whole class because it was declared there:) I hope you understand now ^_^

Alright.
First of all I'd like to thank CloneXpert, very nice help couldnt be happier. Thank you Mate :D
Second, once again you have made my day Daniweb :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.