Get all open WPF windows

20,808

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.

Share:
20,808
Mister S
Author by

Mister S

Updated on July 13, 2022

Comments

  • Mister S
    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 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;
    }
    
    • Konrad Morawski
      Konrad Morawski over 11 years
      Have you debugged it? What is null? System.Windows.Application.Current.Windows? System.Windows.Application.Current?
    • Graham Clark
      Graham Clark over 11 years
      either Application, Current or Windows is null. If you put a breakpoint on the line before, you'll be able to find out in the debugger.
    • Frank Bollack
      Frank Bollack over 11 years
      Is your application a WPF application?
    • Mister S
      Mister S over 11 years
      Yes, 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
      Konrad Morawski over 11 years
      Where 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't getWindow (should be GetWindow by .NET convention, but that's another story) called before the App object is instantiated?
    • dash
      dash over 11 years
      According 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
      Mister S over 11 years
      The 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
      Raj Ranjhan over 11 years
      You can use Process.GetProcesses to get Window handle of another process and then use P/Invoke to send messages to it.
    • Mister S
      Mister S over 11 years
      @Anurag Ranjhan - but how after that cast process type to Window?
    • Raj Ranjhan
      Raj Ranjhan over 11 years
      You can't, it belongs to another AppDomain
    • Mister S
      Mister S over 11 years
      So this solution won't help me.
  • ken2k
    ken2k over 11 years
    How does Window class apply to WinForms?
  • tm1
    tm1 about 5 years
    I 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 from System.Windows.Application. Even if this is the case, you should rather refer to the static property using Application.Current.
  • luka
    luka over 3 years
    The Question is related About WPF , an my answer is for WPF, that's all.