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.

3
  • 3
    If you run VS as administrator, anything you are debugging will be running as admin also.
    – JMK
    Dec 5, 2013 at 15:42
  • I'd recommend the following fix otherwise you have problems running pinned solutions
    – JonnyRaa
    May 15, 2014 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 1, 2018 at 2:18

6 Answers 6

154

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

7
  • 14
    Better yet, set the Visual Studio shortcut to run as administrator from its Properties page Dec 5, 2013 at 15:45
  • 29
    @PanagiotisKanavos I personally like to avoid running things as administrator unless I have a specific reason to do so Dec 5, 2013 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 Jun 15, 2015 at 10:27
  • 7
    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, 2018 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 2, 2019 at 9:26
77

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.

4
  • 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, 2018 at 5: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, 2019 at 14:58
  • What is the manifest? I could not find such a file/option Jun 11, 2022 at 12:49
  • 3
    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. Jun 12, 2022 at 3:48
12

You can also set this administrator option automatically:

enter image description here

4
  • 1
    I think you mean to highlight the "Properties" entry in the context menu, but everyone knows what you're talking about. May 11, 2015 at 17:02
  • An easier way is Compatibility > Run As Administrator.
    – AStopher
    Dec 13, 2016 at 13:00
  • 3
    This is a monumentally bad idea. You really don't want Visual Studio running as administrator constantly.
    – Jammer
    Aug 26, 2017 at 15:56
  • 2
    This screen shot is completely unintelligible, and has no accompanying explanation, Regardless of whether it is a good solution or not. Dec 27, 2018 at 16:54
7

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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.