Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get a list of running applications

Posted on 2011-03-14
9
Medium Priority
?
309 Views
Last Modified: 2012-05-11
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
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1200 total points
ID: 35132005
I would imagine you can just loop through the entire process list looking for processes whose MainWindowHandle is not zero.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (Process p in Process.GetProcesses())
				if (p.MainWindowHandle != IntPtr.Zero)
					Console.WriteLine(p.ProcessName);
			Console.ReadKey();
		}
	}
}

Open in new window


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			List<Process> processes = Process.GetProcesses().Where(x => x.MainWindowHandle != IntPtr.Zero).ToList();
			foreach (Process p in processes)
				Console.WriteLine(p.ProcessName);
			Console.ReadKey();
		}
	}
}

Open in new window

0
 
LVL 14

Assisted Solution

by:systan
systan earned 800 total points
ID: 35132120
The 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;
using System.Diagnostics;

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

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

            Process ThisProcess = null;
            foreach (Process ThisProcess_loopVariable in processes)
            {
                ThisProcess = ThisProcess_loopVariable;
                if (ThisProcess.Id != current.Id)
                {
                    if (!string.IsNullOrEmpty(ThisProcess.MainWindowTitle))
                    {
                        listBox1.Items.Add(ThisProcess.ProcessName);
                    }
                }
            }

        }


    }
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35132200
@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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:systan
ID: 35132350
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

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35132499
systan, that's almost word-for-word the code I just posted (http:#a35132200). ;)
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 35134197
sorry guys i didn't had time to test the solutions, and it's late.  I'll test that tomorrow morning.

thanks
0
 
LVL 14

Expert Comment

by:systan
ID: 35135039
Yes, I told you that it's much better and shorter.  Nice simple code.
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 35137566
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).
0
 
LVL 10

Author Closing Comment

by:Christian de Bellefeuille
ID: 35137576
Thanks a lot to both of you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question