简体   繁体   English

如何在 Visual Studio 调试中以管理员身份运行应用程序?

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

I have ac# application where I have to have read/write access to the root of the C drive.我有 ac# 应用程序,我必须对 C 驱动器的根目录进行读/写访问。 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.但我需要调试它,我不确定如何在 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.现在我有一个工作,但我想知道未来。

Just run visual studio itself as an administrator.只需以管理员身份运行visual studio。 Any program you debug from there will also be run as an administrator.您从那里调试的任何程序也将以管理员身份运行。

VS must be run with admin right. VS 必须以管理员权限运行。 however, a more elegant way is in the requiredExecutionLevel in manifest should set to 'requireAdministrator'.然而,更优雅的方式是在 manifest 中的 requiredExecutionLevel 应该设置为“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.当您打开项目并尝试调试时,VS2012 会警告管理员权限并重新启动为管理员权限。 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.而且 exe 文件将首先被标记为需要管理员权限,因此在部署时您不需要在文件属性中配置管理员权限要求。

You can also set this administrator option automatically:您还可以自动设置此管理员选项:

在此处输入图片说明

要回答标题中的问题,您只需在启动 VS 时从上下文菜单中选择以管理员身份运行即可。

Now the checked answer will not working.现在检查的答案将不起作用。

You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level .您应该在项目属性Linker -> Manifest File -> UAC Execution Level 中找到一个选项。 Set this to requireAdminstrator .将此设置为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.这将导致生成的清单,包括您需要的requestedExecutionlevel默认的,所以你的用户将自动提示提升权限,如果他们不是已经升高。

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.由于当前用户没有足够的权限打开 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您可以通过右键单击 Visual Studio 并在每次打算打开它时选择以管理员身份运行来解决此问题

As a permanent solution ,作为永久解决方案

You can check the compatibility troubleshooting您可以检查兼容性故障排除

  • Right, Click on Visual Studio > select Troubleshoot compatibility.右键,单击 Visual Studio > 选择兼容性疑难解答。
  • 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有关图像的详细步骤,请检查Visual Studio 要求应用程序具有提升的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2017,无法调试或运行该应用程序 - Visual Studio 2017, cannot debug or run the application Visual Studio中的用户调试以管理员身份运行 - Debug as User from Visual Studios run as Administrator 如何强制我的项目在Visual Studio 2013中始终以管理员身份运行? - How to force my project in Visual Studio 2013 to always run as Administrator? 为什么以“以管理员身份运行”运行Visual Studio? - Why run Visual Studio as “Run as Administrator”? 如何在Visual Studio中调试或运行embed window? - How to debug or run in embed window in Visual Studio? 无法在Visual Studio解决方案的调试文件夹中运行应用程序? - Can't run application in debug folder of Visual Studio Solution? Visual Studio安装项目:如何强制卸载程序以管理员模式运行? - Visual Studio Setup Project: How Can I force an Uninstaller to run in Administrator mode? 如何从Visual Studio以调试模式运行NUnit? - How do I run NUnit in debug mode from Visual Studio? 如何在 Visual Studio 中以不同用户身份调试运行项目? - How to debug run a project as a different user in Visual Studio? 如何在Visual Studio中以调试模式运行NUnit? - How to run NUnit in debug mode from Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM