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 where foreach
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;
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With