如何获取当前正在运行的应用程序列表? [英] How can I obtain a list of currently running applications?

查看:46
本文介绍了如何获取当前正在运行的应用程序列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式获取正在运行的(桌面)应用程序列表,然后我想将该列表显示给用户.它应该类似于 Windows 任务管理器中显示的应用程序列表.

I want to programmatically obtain a list of running (desktop) applications, and then I want to display this list to the user. It should be something similar to the application list displayed in the Windows Task Manager.

我如何在 C# 中创建它?具体来说,我需要一种方法来获取当前正在运行的应用程序列表.

How can I create this in C#? Specifically, I need a way to obtain that list of currently running applications.

推荐答案

您可以使用 Process.GetProcesses 方法 提供有关当前在您的计算机上运行的所有进程的信息.

You can use the Process.GetProcesses method to provide information about all of the processes that are currently running on your computer.

然而,这会显示所有正在运行的进程,包括那些不一定显示在任务栏上的进程.因此,您需要做的是过滤掉那些具有空 MainWindowTitle.上面链接的文档解释了为什么会这样:

However, this shows all running processes, including ones that are not necessarily shown on the taskbar. So what you'll need to do is filter out those processes that have an empty MainWindowTitle.The above-linked documentation explains why this works:

一个进程有一个关联的主窗口只有当进程有一个图形界面.如果相关联进程没有主窗口(这样 MainWindowHandle 为零),MainWindowTitle 是一个空字符串("").

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 (so that MainWindowHandle is zero), MainWindowTitle is an empty string ("").

因此,您可以使用类似于以下代码的内容,该代码将打印出(到控制台窗口)任务栏上可见的所有当前正在运行的应用程序的列表:

So, you could use something like the following code, which will print out (to a console window) a list of all currently running applications that are visible on your taskbar:

Process[] processes = Process.GetProcesses();
foreach (var proc in processes)
{
   if (!string.IsNullOrEmpty(proc.MainWindowTitle))
        Console.WriteLine(proc.MainWindowTitle);
}

这篇关于如何获取当前正在运行的应用程序列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆