question

TamBui-8699 avatar image
1 Vote"
TamBui-8699 asked TamBui-8699 edited

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

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-wpfdotnet-runtimedotnet-wpf-xaml
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@TamBui-8699
I make a .Net core 3.1 app following your question description in my Visual Studio Enterprise 2019, but my app run well without the error you got. Did you run your Visual Studio as Administrator? By the way, my setting of User Account Control Settings is default, what is yours?

0 Votes 0 ·

I restarted my VS 2019 ad Administrator and I get the exact same error. My User Account Control Settings are set to "Always Notify". I'm attaching my build output with verbosity set to "Diagnostic". I'm unsure where the 2nd app.manifest is coming from that it is trying to merge mine into.

Link to full txt file (16 MB): https://drive.google.com/file/d/1E0OhEyu91gfvs-HHH3y3eFHyHPHr0Lst/view?usp=sharing
Link to zipped txt file (917 KB): https://drive.google.com/file/d/1grJK7EkMhOMyv7HyobjLynEdkM4y3EXG/view?usp=sharing

I'm on VS 2019 Version 16.9.4. If it matters, this is a XAML Islands application, so the main app is WPF, but there is a UWP project that is used to embed a UWP control into the main WPF app.

Thanks,
Tam

0 Votes 0 ·

I think I have root-caused the issue to XAML Islands.

When I create a fresh WPF .NET Core 3.1 app and create the app.manifest and set it to "requireAdministrator", it compiles just fine.

When I follow the guidance for XAML Islands, as soon as I add the nuget package for "Microsoft.Toolkit.Wpf.UI.Controls" and compile, I get the original error that I posted.


1 Vote 1 ·
Show more comments

0 Answers