2

I am creating an application where when a button is pressed, it will a list of currently running processes, with their icons right beside them.

    private void materialFlatButton6_Click_1(object sender, EventArgs e)
    {

Process[] process = Process.GetProcesses();
        foreach (Process prs in process)
        {
            listBox1.Items.Add(prs.ProcessName + "         (" + prs.PrivateMemorySize64.ToString() + ")");
        }
    }

This is the current code present and it does this.

enter image description here

But I want it to show up like this.

enter image description here

How would I present the icons with the ListBox?

Rohan Pas
  • 177
  • 1
  • 14

1 Answers1

3

Here is how to get the associated icon of the process:

Process[] processes = Process.GetProcesses();
foreach(var thisProcess in processes)
{
   Icon ico = Icon.ExtractAssociatedIcon(thisProcess.MainModule.FileName);
}

If I were you I would use a ListView instead because displaying the icon with a listview is much easier.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Additional information: Access is denied. Error. I realize that would mean this would have to run with administrator privileges, and is that really what I would have to give up in order to add icons? – Rohan Pas Mar 19 '17 at 23:38
  • Sorry but you will need to research the permissions related stuff on your own. Your question was about how to get the icon, and that is how you get the associated icon for a process. Perhaps ask another question relating to permissions and maybe a sysadmin or someone will help you out with that. – CodingYoshi Mar 19 '17 at 23:50
  • sorry you lost me on that last comment. what do you mean? – CodingYoshi Mar 19 '17 at 23:57
  • During execution, I got this error. "Unhanded exception has occurred in your application. A 32 bit processes cannot access modules of a 64 bit process." – Rohan Pas Mar 20 '17 at 00:00
  • That error is thrown because your application is a 32bit application and some of the applications which you want to get the icon for are 64bit applications. See [this](http://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c) answer for more details and [this](http://stackoverflow.com/questions/5497064/c-how-to-get-the-full-path-of-running-process/5497319#5497319) answer for another way of achieving what you want. – CodingYoshi Mar 20 '17 at 00:16