How to Force .NET Application to Run as Administrator using UAC

While working on a desktop application I required admin access to my application. There are chances that you might face this requirement hence I'm sharing this snippet.

So First create windows application if you don’t have one by followingthe below steps.

Click on New Project- Windows-Windows Form Application.-Enter the desired application name and click on OK.

Project

You application will get created with default form 1. I have made some basic changes to make the application more understandable.

application

Now, How to make this application to run as administrator?

To do this we need to add a Manifest File to our project first. Follow the below steps to get manifest file added.

Right click on Project - Add New Item - select "Application Manifest File"

Add

Manifest file will be created.

Now Change the <requestedExecutionLevel> element like below.

code

Comment out the existing line

  1. <requestedExecutionLevel level="asInvoker" uiAccess="false" />  
And add new line as below.
  1. <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
The user gets the UAC prompt when they start the program.

UAC prompt

As we are developing the application in visual studio system we'll restart the visual studio in Administrator mode. So to keep developing and testing the application you will require to run visual studio in Admin access.

We can also check if the exe is asking for admin access or not. For this we will not require visual studio. So just go to the file path where you created application and in applications “\bin\Debug” folder.

You will see exe file of your application.

Just double click on it and you will be asked for user access control.

That’s all. Your application now will ask for admin access.

You can explore more about User access control here and here

Keep learning!! Keep sharing!! Keep caring.

 

Ebook Download
View all
My Ideas
Read by 2 people
Download Now!
Learn
View all