如何在使用 VisualStudio 进行调试时以管理员身份运行应用程序?

我有一个 c # 应用程序,在那里我必须对 C 驱动器的根进行读/写访问。我意识到我可以编译代码并以管理员的身份运行可执行文件,而且它能工作。但是我需要调试它,我不确定如何在 Visual Studio 中启动应用程序。

我试着补充道:

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

但我仍然得到访问拒绝错误。

下面是失败的代码行:

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

现在我有一个工作,但我想知道的未来。

112665 次浏览

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

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

You can also set this administrator option automatically:

enter image description here

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.

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

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.