简体   繁体   English

获取当前正在运行的应用程序列表,而不是进程 c#

[英]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();我知道您可以使用Process[] processes = Process.GetProcesses();获取当前正在运行的Process[] processes = Process.GetProcesses();列表Process[] processes = Process.GetProcesses(); or Process[] processes = Process.GetProcessesByName("processName");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#?有没有办法在 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 .获取进程列表,然后按具有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.如果关联进程没有主窗口,则 MainWindowHandle 值为零。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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