c# Get process window titles

First Second picture First Second · Jul 26, 2013 · Viewed 24.4k times · Source
    proc.MainWindowTitle.Contains("e")

How does one get the window titles of all the current windows containing "e" open instead of just the Main Window using the "MainWindowTitle" and store them into a string array?

EDIT:

     string[] toClose = {proc.MainWindowTitle};
                for (int i = 0; i < toClose.Length; i++)
                {
                    string s = toClose[i];
                    int hwnd = 0;

                    hwnd = FindWindow(null, s);

                    //send WM_CLOSE system message
                    if (hwnd != 0)
                        SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero);

Answer

HuorSwords picture HuorSwords · Jul 26, 2013

Code snippet

string[] result = new string[50];
int count = 0;
Process[] processes = Process.GetProcesses();
foreach(var process in processes)
{
    if (process.MainWindowTitle
               .IndexOf("e", StringComparison.InvariantCulture) > -1)
    {
        result[count] = process.MainWindowTitle;
        count++;
    }
}

Please review this question (and the accepted answer) that guides my answer.

Edit:

If I understand correctly, you want to retrieve de list of processes main window titles.

Analysing your code below, the toClose variable always store one title: the proc.MainWindowTitle value.

string[] toClose = { proc.MainWindowTitle };

You must retrieve each window title using the foreach statement of my original answer. Then, you must use the result variable in the proposed solution instead of toClose variable on your piece of code.