Link to home
Start Free TrialLog in
Avatar of Christian de Bellefeuille
Christian de BellefeuilleFlag for Canada

asked on

Get a list of running applications

I would like to know how i could get a list of applications running on Windows (only those who have a graphical window, not the process list).  

Anyone can help?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@systan

Checking the MainWindowTitle isn't a good idea, it's entirely possible there will be applicatons open with forms that have no title.


@cdebel

If you want to exclude the current process (i.e. you don't want to find yourself), you can just tweak the code slightly:
int myId = Process.GetCurrentProcess().Id;
List<Process> processes = Process.GetProcesses().Where(x => x.Id != myId && x.MainWindowHandle != IntPtr.Zero).ToList();

Open in new window


Or:
int myId = Process.GetCurrentProcess().Id;
foreach (Process p in Process.GetProcesses())
	if (p.Id != myId && p.MainWindowHandle != IntPtr.Zero)
		Console.WriteLine(p.ProcessName);

Open in new window

I think it's not a big deal,  depends where to used it; but it's much better and shorter.  Nice simple code.
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

//for Process.GetCurrentProcess
using System.Diagnostics;

namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


    private void getwinlist()
    {
        int myId = Process.GetCurrentProcess().Id;
        foreach (Process p in Process.GetProcesses())
        if (p.Id != myId && p.MainWindowHandle != IntPtr.Zero)
        listBox1.Items.Add(p.ProcessName);  
    }


        private void Form1_Load(object sender, EventArgs e)
        {
            getwinlist();
        }


    }
}

Open in new window

systan, that's almost word-for-word the code I just posted (http:#a35132200). ;)
Avatar of Christian de Bellefeuille

ASKER

sorry guys i didn't had time to test the solutions, and it's late.  I'll test that tomorrow morning.

thanks
Yes, I told you that it's much better and shorter.  Nice simple code.
ok i've just tried both codes.  They both work.

I'll attribute a bit more points to tgerbert version because i agree with him, an application might not have a window title.  It might be rare, but it's the case (ex: explorer.exe doesn't have any title, but it's important to list it in my case).
Thanks a lot to both of you