Solved

Get a list of running applications

Posted on 2011-03-14
9
269 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now