Thursday, September 10, 2015

Creating A C# Desktop Program That Must Run With Administrator Privilege

The problem

Sometimes you want that your program requires to run with "administrator" privilege. For instance, if your program needs to listen to the TCP port 80.

This means that you would like to have this kind of message to appear at program start to request the user to grant the program elevated privilege.

The solution

It is possible to specify in Visual Studio to specify that an executable must be run with administrator privileges.

To do that you need to perform the 2 following steps:
  1. Add to the solution an "app.manifest" file

    The app.manifest is added to your solution
  2. Modify this file to set the correct value to requestedExectionLevel:

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

Note: When you try to debug such program inside the Visual studio environment, The visual studio shall detect that the administrator privileges are needed and request you to restart Visual studio in administrator mode.

No comments:

Post a Comment