How to run application as administrator in debug with Visual Studio?

C#Visual Studio

C# Problem Overview


I have a c# application where I have to have read/write access to the root of the C drive. I realize I can compile the code and run the executable as administrator and it works. But I need to debug it and I am unsure as to how one would start the app within Visual Studio.

I have tried adding:

<requestedExecutionLevel level="asInvoker" uiAccess="true" />

to my manifest but I still get access denied error.

Here is the line of code that fails:

MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(@"c:\somemapnamefile.data", System.IO.FileMode.OpenOrCreate, "somemapname", 1000);

For now I have a work around but I'd like to know for the future.

C# Solutions


Solution 1 - C#

Just run visual studio itself as an administrator. Any program you debug from there will also be run as an administrator.

Solution 2 - C#

VS must be run with admin right. however, a more elegant way is in the requiredExecutionLevel in manifest should set to 'requireAdministrator'.

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

When you open the project and try to debug, the VS2012 will warn about the admin right and restart itself to admin right. And also the exe file will be marked as requiring admin right at the first place therefore when deploy you don't need to configure admin right requirement in file properties.

Solution 3 - C#

You can also set this administrator option automatically:

enter image description here

Solution 4 - C#

To answer the question in your title, you can just select Run as Administrator from the context menu when starting VS.

Solution 5 - C#

Now the checked answer will not working.

You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level. Set this to requireAdminstrator.

This will cause the default generated manifest to include the requestedExecutionlevel that you need, so that your users will be prompted automatically to elevate their privileges if they are not already elevated.

Solution 6 - C#

The "This task requires the application to have elevated permissions" error occurs because of The current user didn’t have a sufficient privilege to open Visual Studio.

As a temporary solution

You can overcome this issue by right-clicking on visual studio and select run as administrator at every time you intend to open it

As a permanent solution,

You can check the compatibility troubleshooting

  • Right, Click on Visual Studio > select Troubleshoot compatibility.
  • Select Troubleshoot Program.
  • Check The program requires additional permissions.
  • Click on Test the program.
  • Wait for a moment until the program launch. Click Next.
  • Select Yes, save these settings for this program.

> For the detail steps with images, please check Visual Studio requires the application to have elevated permissions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncarny666View Question on Stackoverflow
Solution 1 - C#Sam I am says Reinstate MonicaView Answer on Stackoverflow
Solution 2 - C#Supawat PusavannoView Answer on Stackoverflow
Solution 3 - C#Bura ChuhadarView Answer on Stackoverflow
Solution 4 - C#JoshView Answer on Stackoverflow
Solution 5 - C#JunJie WangView Answer on Stackoverflow
Solution 6 - C#MohamedView Answer on Stackoverflow