0

I want to collect processes in listbox like this:

app.exe otherapp.exe

but I just get:

System.Diagnostics.Process(app.exe) System.Diagnostics.Process(otherapp.exe)

My code:

    private void Form1_Load(object sender, EventArgs e)
    {
        Process[] prs = (Process.GetProcesses());

        foreach (Process pr in prs)
        {
            listBox1.Items.Add(Convert.ToString(pr));
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Process[] prs = Process.GetProcesses();

        string item = Convert.ToString(listBox1.SelectedItem);

        //item.Kill();
    }
wsdsad
  • 15
  • 5
Aatroxon
  • 51
  • 1
  • 1
  • 11

2 Answers2

0

First you should store the process name:

    foreach (Process process in Process.GetProcesses())
    {
        listBox1.Items.Add(process.ProcessName);
    }

After that you can use the name to get the process(es) and kill it/them:

    Process[] processes = Process.GetProcessesByName(listBox1.SelectedItem.ToString());

    foreach (Process process in processes)
    {
        process.Kill();
    }

See https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx for more information about the process class.

Manuel
  • 33
  • 4
0

You can use the same code just a small change

 listBox1.Items.Add(Convert.ToString(pr.ProcessName));

So in short

  private void Form1_Load(object sender, EventArgs e)
{
 Process[] prs = (Process.GetProcesses());

             foreach (Process pr in prs)
             {

                listBox1.Items.Add(Convert.ToString(pr.ProcessName));
             }
}

Output:

enter image description here

Reason:

You just had to use the Property ProcessName property on the Instance

Clint
  • 6,011
  • 1
  • 21
  • 28