112

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.

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
carny666
  • 2,325
  • 2
  • 18
  • 27
  • 3
    If you run VS as administrator, anything you are debugging will be running as admin also. – JMK Dec 05 '13 at 15:42
  • I'd recommend the [following fix](http://stackoverflow.com/questions/12257110/can-you-force-visual-studio-to-always-run-as-an-administrator-in-windows-8) otherwise you have problems running pinned solutions – JonnyRaa May 15 '14 at 11:38
  • In my case, I had to go to Compatability and select run in Windows XP (Service Pack 3). Running the IDE as administrator did not help. – Wendy May 01 '18 at 02:18

6 Answers6

153

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

  • 14
    Better yet, set the Visual Studio shortcut to run as administrator from its Properties page – Panagiotis Kanavos Dec 05 '13 at 15:45
  • 29
    @PanagiotisKanavos I personally like to avoid running things as administrator unless I have a specific reason to do so – Sam I am says Reinstate Monica Dec 05 '13 at 15:46
  • 1
    Yes. There are some couple of programs that requires an administrator. And once, I run it, VS asks to restart the VS in administrator mode. I am not sure how's that happening. I just set the level="requireAdministrator". But sometimes, it does not automatically restart – Jayson Ragasa Jun 15 '15 at 10:27
  • If you set your VS to run as Admin by default, but double-click on a solution, you may end up launching it accidentally as non-admin. Happens to me once in a while. – kayleeFrye_onDeck Oct 05 '17 at 17:24
  • 6
    Despite being accepted, this **is not** the correct solution. You don't run programs, neither VS nor anything else as admin unless it is necessary. The original question was about the program the OP writes, not VS. The correct answer is to specifiy that the program under development requires admin privileges. – Gábor Dec 29 '18 at 11:41
  • 3
    I am with @Gábor : **this is not the correct solution**. You simply set the `requiredExecutionLevel` to `requireAdministrator` in the app manifest, and VS will automagically notice that and, upon starting Debug, will show a dialog where it asks you if you want to restart itself with admin rights. – gog Apr 02 '19 at 09:26
  • Won't this also set every file created from VS with incorrect owner/rights? – Dims Sep 07 '20 at 18:53
76

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.

Supawat Pusavanno
  • 2,968
  • 28
  • 21
  • 1
    Also ensure that under your project's Security tab, "Enable ClickOnce..." is unchecked else you will get a security warning when you try to run your admin-required code. (that setting will be checked if you've already clicked 'Publish' on your app at some point). – monty Apr 12 '18 at 05:42
  • 1
    Just a note - if there is no manifest, simply add one. Somehow I failed to realize it may be non-existent. – Mołot Jul 15 '19 at 14:58
  • What is the `manifest`? I could not find such a file/option – Mehdi Dehghani Jun 11 '22 at 12:49
  • 2
    `manifest` can be added by, right click VS project `properties` tree, then add item (search `manifest`) the file name likes `app.manifest`. After that, open project properties, under Application/Resources/Icon and manifest, choose the manifest file. – Supawat Pusavanno Jun 12 '22 at 03:48
13

You can also set this administrator option automatically:

enter image description here

Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
7

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

Josh
  • 1,309
  • 2
  • 17
  • 37
7

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.

JunJie Wang
  • 460
  • 3
  • 10
2

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

Mohamed
  • 806
  • 13
  • 30