?
Solved

Get a list of running applications

Posted on 2011-03-14
9
Medium Priority
?
307 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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