dotnet / core

.NET news, announcements, release notes, and more!
https://dot.net
MIT License
20.76k stars 4.86k forks source link

Using UI Automation without installing Desktop Runtime #4714

Open Jasdo opened 4 years ago

Jasdo commented 4 years ago

Hi, We need to use UI automation, and in order to run this code (just a simple example):

static void Main(string[] args)
{
   AutomationElement currentProcessWindow =
      AutomationElement.RootElement.FindAll(TreeScope.Children, Condition.TrueCondition)
          .OfType<AutomationElement>()
          .Single(a => a.Current.Name.Contains(Process.GetCurrentProcess().ProcessName));

   Console.WriteLine(currentProcessWindow.Current.ControlType.ProgrammaticName);
}

we need to reference Microsoft.WindowsDesktop.App.Wpf:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.Wpf" />
  </ItemGroup>

</Project>

This requires to install Desktop Runtime on the machine.

Regardless of the reason, is there a way to use UI automation without having to install Desktop Runtime?

Not sure that this is the right location for this. Tried also here, didn't get a response.

Thanks.

svick commented 4 years ago

You could publish your app as self-contained. That way, the Desktop Runtime won't have to be installed on the target machine, because it will be deployed along with the app. Though this also significantly increases the size of the app. For your trivial example, the increase is from 180 kB to 150 MB.

If that's a problem for you, you could try trimming. In this case, that decreases the size to 87 MB.