Solved

Get a list of running applications

Posted on 2011-03-14
9
298 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
  • 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
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

 
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

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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