You're trying to use UIA's caching feature, which allows you to cache AutomationElements
and their properties and improve performance upon accessing them. This is an advanced feature obviously unrelated to your question, as you'll first need to actually get the AutomationElements
from the UI tree.
Also, your code, even if it worked, wouldn't have returned all the tree, just the child elements of the desktop (in UIA, "child elements" actually mean elements found just one hierarchy under the particular element). In order to navigate on the UI tree, you'll want to use the TreeWalker
class. There's fairly good documentation on it on MSDN, so I suggest you go there first before you start using it.
Note that if you want to map your own application and not the entire UI tree, you'll need to instantiate the TreeWalker
object with a Condition that will return your application's main window.
One last note - calling ToString()
on an AutomationElement
wouldn't return anything of particular interest. If you want to get the name of the control or any other property, you'll want to use the GetCurrentPropertyValue
method (or access Current.{PropertyName}
).