Solved

Get a list of running applications

Posted on 2011-03-14
9
306 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
Question by:cdebel
[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 300 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 200 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
Independent Software Vendors: 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!

 
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:cdebel
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:cdebel
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:cdebel
ID: 35137576
Thanks a lot to both of you
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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