How to run application as administrator in debug w

2019-01-17 14:00发布

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.

6条回答
劳资没心,怎么记你
2楼-- · 2019-01-17 14:08

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

查看更多
聊天终结者
3楼-- · 2019-01-17 14:11

This error occurs because of The current user didn’t have a sufficient privilege to open Visual Studio.

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

Also, you can check the compatibility troubleshooting as permanent solution

  • 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 till the program launch.Click Next.
  • Select Yes, save these settings for this program.
  • Wait for resolving the issue.
  • Make sure the final status is fixed.Click Close.

To find the detail steps with image for How to apply that check this link

Hope it helps you

查看更多
Juvenile、少年°
4楼-- · 2019-01-17 14:14

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-17 14:25

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

查看更多
Ridiculous、
6楼-- · 2019-01-17 14:25

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.

查看更多
干净又极端
7楼-- · 2019-01-17 14:31

You can also set this administrator option automatically:

enter image description here

查看更多
登录 后发表回答