Simon Cankov
Simon Cankov

Reputation: 61

How to add a manifest file to my C# program in Visual Studio 2019?

I have a simple application I want to force to run in admin mode. For that, I need to edit the manifest file. I saw tutorials that show adding it by clicking on the "Project-> Add - > new Item," but there is no manifest file there for me.

I tried making my own file (and setting it from project properties as the manifest file) but I don't know what to write there so it doesn't crash.

Manifest file missing

Upvotes: 4

Views: 5308

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51894

Here is a step-by-step 'hack' that should work; it allows you to take a copy of the default embedded manifest and modify that accordingly.

First, add a new XML file to your project via the "Add -> New Item ..." command and call it (say) "Elevate.xml". This will, ultimately, be the custom manifest.

Next, to get the current (embedded) manifest, (re)build your project and open the target (.exe) file from within Visual Studio. This should open that file in "Resource Explorer" mode, similar to the below image:

enter image description here

In that explorer, open the "RT_MANIFEST" node and double-click on the revealed "1 (Neutral)" sub-node; this will open a new window with the program's embedded current (default) manifest displayed as a binary resource. The manifest is actually in XML text format, and you can select that text from the right-most column (ignoring a few leading/trailing non-printable data), as show here:

enter image description here

Copy that selection, open your earlier "Elevate.xml" file, remove all its existing content and "Paste" the manifest resource data; this will give you content similar to that shown below:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

You can then change the "asInvoker" level to "highestAvailable", save that file and rename it (in the project tree) to "Elevate.manifest". You can then select that custom manifest in the project's properties:

enter image description here

I have tested this method on a local (but very trivial) sample program, and it seems to work: when I run the program, the system does ask me for Administrator credentials. If you have any issues, then maybe there are some minor tweaks required in your case.

Upvotes: 5

Related Questions