简体   繁体   中英

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

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();

Amy's answer is the one, but I had to change it to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM