Get all open WPF windows
Solution 1
This is how you cycle through all opened windows in an running application in WPF:
foreach (var Window in App.Current.Windows)
{
// TODO: write what you want here
}
If you want know in windowforms use application instead of app. bye.
Solution 2
Either Current
or Windows
is null
The Windows property can only be access from the thread that created the Application object and this will only work in a WPF application AFTER the application object has been created.
Solution 3
Bare in mind that System.Windows is a namespace, and Application is the actual class that references the current application context. What this means is that ´Application.Current.Windows´ only references all windows spawned by the application itself. Try to loop through all windows and print their title.
What happens in your program, is that the if statement will always be false, unless Title is equal to a window spawned by the Application, thus windowObject will remain to be null, and null will be returned by the method.
Mister S
Updated on July 13, 2022Comments
-
Mister S almost 2 years
I'm trying to get all open windows. I tried to use
System.Windows.Application.Current.Windows
but I get Null Pointer Exception in line whereforeach
loop is. Do anyone has idea what is wrong?public Window getWindow(String Title) { Window windowObject = null; Console.WriteLine("Inside getWindow"); foreach (Window window in System.Windows.Application.Current.Windows) { if (window.Title == Title) { windowObject = window; } } return windowObject; }
-
Konrad Morawski over 11 yearsHave you debugged it? What is
null
?System.Windows.Application.Current.Windows
?System.Windows.Application.Current
? -
Graham Clark over 11 yearseither
Application
,Current
orWindows
is null. If you put a breakpoint on the line before, you'll be able to find out in the debugger. -
Frank Bollack over 11 yearsIs your application a WPF application?
-
Mister S over 11 yearsYes, it is WPF application. I'm using White framework to automate Windows GUI but I need to get window which belong to "Desktop" so I need to switch from application which I run to the Desktop window. P.S. I debugged it and Current is null.
-
Konrad Morawski over 11 yearsWhere are you calling that method? Is the entry point of your WPF application non-standard? If you start it in debug mode going step by step (
F11
), where does it get you? Isn'tgetWindow
(should beGetWindow
by .NET convention, but that's another story) called before theApp
object is instantiated? -
dash over 11 yearsAccording to this MSDN forum question: social.msdn.microsoft.com/Forums/en/wpf/thread/… then this method will work as long as you create an application object in your application startup code.
-
Mister S over 11 yearsThe application is launched by Application.Launch method which comes with White Framework. I haven't found any way to get other opened windows after application launch using White Framework - it only finds launched window. That's the reason why I tried to use System Property. Any suggestions?
-
Raj Ranjhan over 11 yearsYou can use Process.GetProcesses to get Window handle of another process and then use P/Invoke to send messages to it.
-
Mister S over 11 years@Anurag Ranjhan - but how after that cast process type to Window?
-
Raj Ranjhan over 11 yearsYou can't, it belongs to another AppDomain
-
Mister S over 11 yearsSo this solution won't help me.
-
-
ken2k over 11 yearsHow does Window class apply to WinForms?
-
tm1 about 5 yearsI don't see a substantial difference beyond syntax sugar compared to the code already present in the question. Actually, your code only works if you have an
App
class in your project derived fromSystem.Windows.Application
. Even if this is the case, you should rather refer to the static property usingApplication.Current
. -
luka over 3 yearsThe Question is related About WPF , an my answer is for WPF, that's all.