How to retrieve (list) the titles and process id of all the opened applications in the taskbar of Windows with C# in WinForms

In some applications, for example a couple of screenshot tools written, require in order to create a screenshot from a specific window, the current title of the window, as they probably weren't written in C#, so they didn't check up for the process ID or something that could make the filtering easier.

If you are using one of those CLI tools as dependency of your C# Tool, you can list all the processes that have a Window on the taskbar, so you can obtain the title from all of them, so you will be able to extract the title to create your screenshot.

This can be done easily by obtaining a list of all the processes of Windows and verifying whether they have a Window Title or not:

// 1. Import System Diagnostics
using System.Diagnostics;

// 2. Create a list of the active processes of Windows
Process[] processlist = Process.GetProcesses();

// Iterate over them
foreach (Process process in processlist)
{
    // If the process appears on the Taskbar (if has a title)
    // print the information of the process
    if (!String.IsNullOrEmpty(process.MainWindowTitle))
    {
        Console.WriteLine("Process:   {0}", process.ProcessName);
        Console.WriteLine("    ID   : {0}", process.Id);
        Console.WriteLine("    Title: {0} \n", process.MainWindowTitle);
    }
}

The previous snippet would print the following output in the terminal:

Process:   chrome
    ID   : 11896
    Title: c# - get the titles of all open windows - Stack Overflow - Google Chrome 

Process:   WinStore.App
    ID   : 15348
    Title: Microsoft Store 

Process:   SystemSettings
    ID   : 11020
    Title: Settings 

Process:   ApplicationFrameHost
    ID   : 8848
    Title: Settings 

Process:   Code
    ID   : 7852
    Title: ? Untitled-2 - electron-quick-start - Visual Studio Code 

Process:   WindowsInternal.ComposableShell.Experiences.TextInput.InputApp
    ID   : 13864
    Title: Microsoft Text Input Application 

Process:   devenv
    ID   : 16860
    Title: Sandbox (Running) - Microsoft Visual Studio 

Process:   Sandbox
    ID   : 15524
    Title: Form1 

Process:   NVIDIA Share
    ID   : 13076
    Title: NVIDIA GeForce Overlay 

And that's it, you should now be able to filter by the Window Title of all the opened applications in Windows.

This could interest you

Become a more social person