Search code examples
white-frameworkmicrosoft-ui-automation

Finding all windows on desktop using UIAutomation .NET


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.


Solution

  • 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);