How to use AutomationElement in a .NET Core 8 console application?
See the question and my original answer on StackOverflowYou can just add a reference to the Windows COM objects that implement UIA:
Or just copy paste these COMReference XML elements into your .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<COMReference Include="UIAutomationClient">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>944de083-8fb8-45cf-bcb7-c477acb2f897</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>false</EmbedInteropTypes>
</COMReference>
<COMReference Include="UIA">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>930299ce-9965-4dec-b0f4-a54848d4b667</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>false</EmbedInteropTypes>
</COMReference>
</ItemGroup>
</Project>
Note you might want to set EmbedInteropTypes
to false
depending on warnings you may face during coding.
And then you'll be able to use it simply like this:
using UIAutomationClient; // use this namespace to start
namespace UIAOpenTray;
internal class Program
{
static void Main(string[] args)
{
var automation = new CUIAutomation8();
}
}
PS: there's no need to reference WPF, but this is a bit different than using the System.Windows.Automation namespace, although very similar.