Finding all windows on desktop using UIAutomation .NET

user96403 picture user96403 · Apr 17, 2012 · Viewed 9.4k times · Source

I am trying to find all desktop windows using .NET UIAutomation OR White framework. I tried :

1.)

            AutomationElement rootElement = AutomationElement.RootElement;
            var winCollection = rootElement.FindAll(TreeScope.Subtree, Condition.TrueCondition);

2.)

            Desktop.Instance.Windows();

Both throw ArgumentException. Please let me know if there are other ways to do this...

UPDATE/ANSWER: Desktop.Instance.Windows(); works fine except that it throws exception while debugging the code using VS2010.

Answer

Bill Agee picture Bill Agee · Apr 17, 2012

Using TreeScope.Children should work if you want to access the immediate child elements of the desktop ::

    AutomationElement rootElement = AutomationElement.RootElement;
    var winCollection = rootElement.FindAll(TreeScope.Children, Condition.TrueCondition);

    foreach (AutomationElement element in winCollection)
        Console.WriteLine(element.Current.Name);