Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

arrays - c# Get process window titles

    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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...