Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 374
carny666 Avatar asked Dec 05 '13 15:12

carny666


People also ask

How do I Debug as administrator in Visual Studio?

Run Visual Studio as an administratorOpen the Start menu, and scroll to Visual Studio. From the right-click or context menu of Visual Studio 2019 or Visual Studio 2022, select More > Run as administrator.

How do I always run as administrator in Visual Studio?

Permanent or Always: For this go to startup search bar, search for visual studio, right click to it and go to properties. In the properties click on advanced button and check the Run as Administrator check box and then click on ok.

How do I force my .NET application to run as administrator?

Right click your executable, go to Properties > Compatibility and check the 'Run this program as admin' box.


2 Answers

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

like image 178
Sam I am says Reinstate Monica Avatar answered Sep 22 '22 14:09

Sam I am says Reinstate Monica


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.

like image 38
Supawat Pusavanno Avatar answered Sep 24 '22 14:09

Supawat Pusavanno