locked
Using UIA to access UI elements representing files and folders in a Windows Explorer window RRS feed

  • General discussion

  • I'm posting this in response to a query at the UIA blog relating to how to use UIA (from C# code) to access the UI for files and folders.


    The UI Automation (UIA) API can be used to find details about the items shown in a Windows Explorer window, and also about the item that currently has keyboard focus. The Inspect SDK tool uses the native-code UIA API that comes with Windows 7, and can be used to examine all the UI that's shown on your screen. So I'd first suggest that you try using Inspect to get a feel for the sort of data exposed by the UI you're interested in. For example, the image below shows Inspect examining an item in the Windows Explorer window. On the right side of Inspect it shows the details about the element with focus, and on the left, it shows the relationship between that element and other elements around it.

    It's worth pointing out that the UIA API knows nothing about files systems, rather it works with UI that's shown on the screen. So to figure out the location of the file represented by the UI shown for the item on the screen, I think you may need to combine the details of that item with the details for the address bar shown above it.

    The UIA API has a function called ElementFromFocus(), which gets the UI element which currently has keyboard focus. So if keyboard focus is in a Windows Explorer window, you can get details about the currently focused file or folder.

    I've built a number of UIA sample apps in C#. These samples uses a COM wrapper which allow my C# code to call into the native-code UIA API. (Note that the samples are not using the managed version of the UIA API which is included in the .NET Framework.) You may want to download some of these samples, and debug them to see how the apps use UIA to get information about the UI shown on the screen.

    For example, the sample at http://code.msdn.microsoft.com/windowsdesktop/Windows-7-UI-Automation-6390614a gets information about the currently focused UI element, and the sample at http://code.msdn.microsoft.com/windowsdesktop/Windows-7-UI-Automation-0625f55e gets information about hyperlinks shown in a browser window.

    The whole set of samples I've built is at http://code.msdn.microsoft.com/windowsdesktop/site/search?f%5B0%5D.Type=User&f%5B0%5D.Value=Guy%20Barker%20MSFT.

    Once you've tried out these sample apps and used Inspect to examine the UI that you're interested in, let me know if you have specific questions about how to interact with the UI that you need.

    Thanks,

    Guy

    Wednesday, June 13, 2012 2:37 PM