4

I'm developing an application in Visual Studio which will require administrator permissions after it is installed on a user's system. I've added a proper application manifest file that will make sure this happens:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

However now if I want to debug the program in Visual Studio, I am required to run Visual Studio as an administrator. No bueno. And the most annoying part is that the program doesn't really need administrator privileges during debugging. It only requires those privileges after being properly installed on a user's system.

Is there a clean, easy way to "throw a switch" that disables the administrator requirement during development, and then re-enables it when I release the application?

EDIT:

When I release the application to the user, I run a build script which uses MSBuild. What I'm trying to do is make Visual Studio build a solution in the Release configuration without the administrator manifest. Then when I run my build script, I want to get MSBuild to include the administrator manifest while keeping the same Release configuration.

Phil
  • 6,561
  • 4
  • 44
  • 69
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35307/discussion-between-phil-and-j) – Phil Aug 12 '13 at 18:55
  • @J... Thanks for your help. I appreciate it. I'm going to delete most of my comments here; you may wish to do the same. – Phil Aug 12 '13 at 20:15
  • Done. I've added the remaining reference links to the answer. Glad it worked out! – J... Aug 12 '13 at 20:31

2 Answers2

3

You can do this with pre-build events Project -> Options -> Build Events

Make a pre-build event like :

del "$(ProjectDir)app.manifest"

if "$(ConfigurationName)"=="Debug" (
  copy "$(ProjectDir)app.debug.manifest" "$(ProjectDir)app.manifest"
) else (
  copy "$(ProjectDir)app.release.manifest" "$(ProjectDir)app.manifest"
)

Here we're assuming your manifest is called app.manifest and is in the root directory for your project - obviously modify it as needed.

In any case, make two copies of your manifest file and call one app.debug.manifest and one app.release.manifest, configure them appropriately, and this will replace the active app.manifest with the appropriate version before each build.

EDIT:

If you want MSBuild to use the same configuration but a different manifest than Visual Studio, you may also try tweaking the above pre-build event to look like this:

del "$(ProjectDir)app.manifest"

if "$(AdminManifest)"=="true" (
  copy "$(ProjectDir)app.release.manifest" "$(ProjectDir)app.manifest"
) else (
  copy "$(ProjectDir)app.debug.manifest" "$(ProjectDir)app.manifest"
)

Now when you run MSBuild, you can do it like so:

MSBuild.exe MySolution.sln /p:Configuration=WhateverYouWant /p:AdminManifest=true

see also :

https://stackoverflow.com/a/938888/327083

https://stackoverflow.com/a/1732478/327083

Community
  • 1
  • 1
J...
  • 30,968
  • 6
  • 66
  • 143
3

This is how I ended up doing this a few years later (with probably many changes to csproj files and MSBuild):

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <ApplicationManifest>app.debug.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
Biesi
  • 800
  • 9
  • 17