Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all open WPF windows

Tags:

c#

windows

wpf

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;
}
like image 492
Mister S Avatar asked Aug 29 '12 11:08

Mister S


2 Answers

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.

like image 150
luka Avatar answered Sep 20 '22 17:09

luka


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.

like image 37
Emond Avatar answered Sep 21 '22 17:09

Emond