使用UIAutomation .NET在桌面上查找所有窗口

use*_*403 7 white-framework microsoft-ui-automation

我试图使用.NET UIAutomation OR White框架找到所有桌面窗口.我试过了 :

1.)

            AutomationElement rootElement = AutomationElement.RootElement;
            var winCollection = rootElement.FindAll(TreeScope.Subtree, Condition.TrueCondition);
Run Code Online (Sandbox Code Playgroud)

2.)

            Desktop.Instance.Windows();
Run Code Online (Sandbox Code Playgroud)

两者都抛出ArgumentException.如果还有其他方法可以让我知道......

更新/答案:Desktop.Instance.Windows(); 工作正常,但它在使用VS2010调试代码时抛出异常.

Bil*_*gee 5

TreeScope.Children如果您要访问桌面的直接子元素,则使用应该可以使用::

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

    foreach (AutomationElement element in winCollection)
        Console.WriteLine(element.Current.Name);
Run Code Online (Sandbox Code Playgroud)