-2

I am currently search a way to get the titles/names of all open windows (from a specific process if possible) in c#. So a process can have multiple windows an I just found the Process.MainWindowTitle command, but I want the names of all the windows that run under this process, not just the MainWindowTitle. Is there a command/a way to get all window titles in a list/array? I searched through the internet but couldn't find anything that worked or I understand. It would be awesome if somebody has a way to do it and could explain it to me.

Thanks Akira

Edit: I found this link (http://csharphelper.com/blog/2016/08/list-desktop-windows-in-c/) but I couldn't get it working and I don't understand what's going on there. So if anybody understand this...or if not just get it working to print out all titels in the console. I would be so thankful.

29thDay
  • 83
  • 1
  • 9

1 Answers1

1
using System;
using System.Diagnostics;
using System.Linq;  

//will only get process with main window title property that is not empty

Process[] processlist = Process.GetProcesses();
var processTitle = processlist.Where(c => !string.IsNullOrEmpty(c.MainWindowTitle)).Select(c => c.MainWindowTitle).ToList();

.Where(c => !string.IsNullOrEmpty(c.MainWindowTitle) is the part where you only select the main window title who is not an empty string. Then the Select(c => c.MainWindowTitle) only gets the MainWindowTitle property of each Process. ToList list all the values that match the Where condition to a List<string> because the property of MainWindowTitle is string

updated answer, almost still the same.

string processname = ""; //the process you want to capture
Process[] processlist = Process.GetProcesses();
var processTitle = processlist.Where(c => c.ProcessName == processname).ToList();

still same explanation, now you supply on which process (c.ProcessName == processname will now only retrieve the specific process name you supplied.) you want to capture and you can now debug/capture the details you need.

or refer to this explanation

https://stackoverflow.com/a/17890354/2122217

Gabriel Llorico
  • 1,783
  • 1
  • 13
  • 23