Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

process - Getting a list of current APPLICATIONS running, not processes c#

I know you can get a list of the current processes that are running by using Process[] processes = Process.GetProcesses(); or Process[] processes = Process.GetProcessesByName("processName");

However I need to grab the current applications that are running, not necessarily the specific processes. The reason is because sometimes there are processes that run in the background that relate to a certain application however the actual application itself is not running. But for my purposes I need to know if the actual application itself is running.

Is there a way to do this in C#?

Edit: Apparently I haven't made myself clear. For example in Task Manager you can see a list of Applications that are currently running, as well as a list of Processes that are currently running. I'm trying to grab the list of Applications that one can see in Task Manager, not the extensive list of processes

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Get the list of processes, then filter by those processes that have a MainWindowHandle.

A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.

If it has a main window, it's an "application" as far as the Task Manager is concerned.

var processes = Process.GetProcesses()
    .Where(p=> p.MainWindowHandle != 0)
    .ToArray();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...