【发布时间】:2020-06-08 20:27:14
【问题描述】:
我正在尝试使用 UIAutomation 访问文件夹,但未检测到窗口元素。
当我使用 UI Spy 检查时,它会显示带有类名和进程 ID 的元素。我正在寻找的元素是窗口元素,它属于资源管理器进程。因此,当我尝试使用以下代码时,它返回 0 个元素。我附上了图片以供参考。请帮帮我。
Process[] windowFolders = Process.GetProcessesByName("explorer");
foreach (Process proc in windowFolders)
{
Console.WriteLine(proc.GetType());
proc.Refresh();
Console.WriteLine(proc.MainWindowHandle);
if (proc.MainWindowHandle.ToInt32() != 0)
{
AutomationElement windowExplorer = AutomationElement.FromHandle(proc.MainWindowHandle);
AutomationElementCollection ewindows = windowExplorer.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.window));
foreach (AutomationElement ewindow in ewindows)
{
Console.WriteLine("Window Name: " + ewindow.Current.Name + " Window class name: " + ewindow.Current.ClassName);
}
}
}
【问题讨论】:
-
当自动化暴露
RootElement属性时,您为什么要摆弄进程、主窗口等:“获取当前桌面的根 AutomationElement。”? -
@Damien_The_Unbeliever 非常感谢。使用了根元素属性,它帮助我找到了窗口元素。
标签: c# ui-automation