How do I force a WPF .NET Core 3.1 app to run as administrator?

Tam Bui 11 Reputation points
2021-05-20T01:52:03.183+00:00

In .NET Framework, it was as simple as adding an app.manifest to your project and then adding the "requireAdministrator" line:

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
  <security>
    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
      <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
    </requestedPrivileges>
  </security>
</trustInfo>

But in .NET Core, when I add the app.manifest file to my project, and then ensure the .csproj file contains

<ApplicationManifest>app.manifest</ApplicationManifest>

it gives me a compilation error:

c1010001 Values of attribute "level" not equal in different manifest snippets.

This errors occurs because it is trying to issue an "mt.exe" command to merge my app.manifest with one that resides in obj\x64\Debug\netcoreapp3.1\Manifests\app.manifest. When I go to look at that file, it contains a request level set to "asInvoker" so hence the error. However, if I completely delete my obj folder, this file gets regenerated all the time, and I don't know where it is coming from.

All I want to do is to force my WPF app to run as admin. Any help would be greatly appreciated.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,769 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,158 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.