How can I obtain a list of currently running applications?
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 开发者_StackOverflow社区to the application list displayed in the Windows Task Manager.
How can I create this in C#? Specifically, I need a way to obtain that list of currently running applications.
You can use the Process.GetProcesses
method to provide information about all of the processes that are currently running on your computer.
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:
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);
}
精彩评论