Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force my project in Visual Studio 2013 to always run as Administrator?

I have a WPF project in Visual Studio 2013, this project have two buttons. The first button say Start Service and the second say Stop Service. When I run my Visual Studio as Administrator, the buttons work. But when I open my Visual Studio without privilages, the InvalidOperationException exception appear.

How to force my project start with privilages when Visual Studio doesn't run as administrator?

I added app.manifest to my project and change for

level="requireAdministrator" uiAccess="false"/>

but it didn't function.

For start or stop my service, I am using ServiceController.

like image 869
CampDev Avatar asked Apr 01 '14 21:04

CampDev


People also ask

Do you need admin rights to run Visual Studio?

You can do nearly everything in the Visual Studio IDE as a normal user, but, you need administrator permissions to complete the following tasks: Installing Visual Studio. Upgrading from a trial edition of Visual Studio. Installing, updating, or removing local Help content.

How do I stop Visual Studio code from running as administrator?

Right click on the icon and go to properties. After that just go to the Compatibility windows and in the Privilege Level un-check: "Run these program as administrator".

How do I run as administrator?

Press and hold down the SHIFT key while you right-click the executable file or the icon for the application, and then select Run as. Select The following user. In the User name and Password boxes, type the administrator account and password, and then select OK.

How do I keep something from running as administrator?

You right-click the .exe file, go to properties, then click on the "shortcut" tab and click on "advanced" - then uncheck "run as administrator".


2 Answers

As Torben M. Philippsen mentions in his article:

  • In Visual Studio 2010 (I guess the same applies to VS2008, but I haven’t tested it) right click Your project and select “add new item”
  • Add a application manifest file – the default name will be app.manifest.
  • Inside the manifest file, change the existing configuration from

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

    To

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    
  • Save and close the manifest file.

  • Please note that Your manifest file won’t show up anywhere in your solution. In order to fix that, in solution explorer, click the “show all files” button.
  • Important: Right click the manifest file and add it to the project – we need that in order to tell VS to use the manifest file when compiling our application.
  • Right click Your project and select “properties”.
  • On the application tab, the bottom section, select the manifest file:

Selecting manifest file

manifest file selection

  • Compile and run the application. If Your UAC settings are enabled, You will be prompted to allow the application to start in elevated mode.

  • Sometimes it can come in handy to check whether Your application is actually running in elevated mode or not. Maybe You will find this codesnippet usefull:

     WindowsPrincipal myPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
     if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) == false )
     {
         //show messagebox - displaying a messange to the user that rights are missing
         MessageBox.Show("You need to run the application using the \"run as administrator\" option", "administrator right required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
     }
     else
     {
         MessageBox.Show("You are good to go - application running in elevated mode", "Good job" ,MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
    
like image 76
Mehrad Avatar answered Sep 27 '22 15:09

Mehrad


This is interesting and it seems you need to change permissions of how the project runs, Try doing the following

  • go to project properties > Security
  • enable click-once security settings and select Full trust application

More infor in this link WPF security

like image 43
voddy Avatar answered Sep 27 '22 15:09

voddy