carny666
carny666

Reputation: 2410

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

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.

Upvotes: 120

Views: 138254

Answers (7)

mikekidder
mikekidder

Reputation: 923

I have come from the FUTURE to answer this question. Actually, its 2024 and haven't been on stackoverflow in a LONG time.

OK, call me crazy but.. but I dont want to run this everytime as Administrator.

Simple solution -- Hold down the Ctrl+Shift keys when launching Visual Studio. That will give you the User Account Control panel.

I do this for Windows Terminal when I need it as well.

Upvotes: 2

Mohamed
Mohamed

Reputation: 822

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

Upvotes: 2

JunJie Wang
JunJie Wang

Reputation: 470

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.

Upvotes: 8

Supawat Pusavanno
Supawat Pusavanno

Reputation: 3356

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.

Upvotes: 82

Bura Chuhadar
Bura Chuhadar

Reputation: 3751

You can also set this administrator option automatically:

enter image description here

Upvotes: 12

Josh
Josh

Reputation: 1356

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

Upvotes: 7

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

Upvotes: 166

Related Questions