How To Make .NET Application To Run As Administrator

As a .Net client-server application developer, sometimes you may need to force your application to run as administrator. Though it is not an ideal solution to run an application always as an administrator, you may need it in certain unavoidable situations. One among the way is to add an application manifest to your application. Here I have provided the steps to add a manifest to the application and set the level to requireAdministrator, to force it to run as administrator. In addition, I have also provided a workaround to bypass the UAC prompt while launching the application.

Tools And Technologies Used

I have used C# windows forms client-server application developed using Visual Studio 2017 and installed on Windows 10 for this illustration.

Steps To Force .Net App To Run As Administrator

  1. Go to Visual Studio Solution Explorer, right-click the project and select Add >> New Item….
    .NET App To Run As Administrator - Add manifest
  2. In the Add New Item window, select Application Manifest File and press Add.
    .NET App To Run As Administrator - Add manifest
  3. Now, go back to solution explorer and under the project, find the .manifest file and open it.
  4. Find the requestedExecutionLevel element and change the level to requireAdministrator. <requestedExecutionLevel level=”requireAdministrator” uiAccess=”false” />
    .NET App To Run As Administrator - Modify manifest

You are done with the application part. Now, your application will be forced to run as administrator. However there is a disadvantage. On launching the exe, it will get the User Account Control (UAC) prompt. The application will open only if you choose Yes in the UAC prompt. I will be annoying to respond to UAC prompt every time you run the application. You can follow the below steps to avoid the UAC prompt the specific application. This workaround uses the task scheduler to bypass the UAC prompt.
.NET App To Run As Administrator - UAC Prompt

Steps To Avoid UAC Prompt

  1. Go to Task Scheduler and right-click Task Scheduler Library folder and select Create Task from the context menu.
  2. In the Create Task window, enter name, description and select Run with highest privileges.
    .NET App To Run As Administrator - Create Scheduler Task
  3. Now, go to the Actions tab and press New… button.
  4. In the New Actions window, select the action as Start a program and browse the .Net application you want to run as administrator. Once selected, press OK to add the action.
    .NET App To Run As Administrator - Create Scheduler Task
  5. Now, go to Settings tab and make sure the option Allow task to be run on demand is selected.
  6. Finally, press OK to create the task.
  7. The scheduler task is now ready. You can run the task to run the application without UAC prompt.
    .NET App To Run As Administrator - Create Scheduler Task
  8. Instead of going to task scheduler and run the task to launch the application, you can create a shortcut of the task.
  9. To create a shortcut, right-click the desktop and from the context menu select New >> Shortcut.
  10. In the Create shortcut window enter schtasks /run /TN “{{NAME OF SCHEDULER TASK}}” in the location field and press Next. (NOTE: Replace {{NAME OF SCHEDULER TASK}} with the actual name of the scheduler task which you have created. The name should be inside double quotes.)
    .NET App To Run As Administrator - Create Shortcut
  11. Type in the name of the shortcut and press Finish.

Now, you can double-click the shortcut to open the application without UAC prompt.

Resource

  • About creating and embedding application manifest at MSDN.


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.