'c sharp exe should ask for "run as administrator" prompt when opened
I am having an exe generated by a c sharp program. when i run the exe, i want an UAC prompt to appear with an option to run the exe as administrator. I have seen examples of running an exe as admin by default. But, how do i enable the UAC to ask me for running the exe as admin? Any idea?
Solution 1:[1]
Windows vista/7/server 2008 R2 uses User Account Control (UAC) technology for security and protecting the OS from malware by limiting the privileges of an application until authorized by administrator. This is the reason why we need to make our application run as administrator. This is usually done while running the application. The process is, right click on the application and click on "Run as administrator" to run the application with administrative privileges. If suppose the user forgets to do so and runs the application normally, then unexpected behavior may be seen. Because all the actions which requires administrative privilege in win vista and above (e.g. invoking a process, using system drive, etc.) would not be performed.
So to avoid running the application as administrator every time, we can make our application run as administrator by default. To achieve this we need some tweaking with the application manifest. An application manifest is an XML file that describes an application.
Follow the steps to make your .net application run as administrator:
Step 1: Go to properties of the project and click on the "View Windows Settings" button. This will open the file "app.manifest".
Step 2: In requestedExecutionLevel key change the value of level as "requireAdministrator" and the value of uiAccess as "False". Setting level as "requireAdministrator" means the application runs only for administrators and requires that the application be launched with the full access token of an administrator. Setting uiAccess as "False" means the application does not need to drive input to the user interface of another window on the desktop. Applications that are not providing accessibility should set this flag to false. Applications that are required to drive input to other windows on the desktop (on-screen keyboard, for example) should set this value to true.
Step 3: Save the changes, rebuild your application and install it on Win Vista or higher box.
Now, the application would start automatically as administrator.
Solution 2:[2]
You need to add "Application Manifest File". Step-By-Step
- Right-Click on your project
- Add "New Item"
- Select "Application Manifest File"
- Find this
- change level="requireAdministrator"
Solution 3:[3]
in visual studio, add an "Application Manifest File" (if you don't have one),
then add <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
in it, to request administrator privileges when running the application.
Solution 4:[4]
You can use the ProcessStartInfo class or Windows principal to achieve this.
You can also embed a manifest file in your application .exe and set requestedExecutionLevel
property.
There is also a way to enable/disable UAC trough the windows registry.
Solution 5:[5]
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
I used this property !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Harsh |
Solution 2 | Pongsathon.keng |
Solution 3 | Orentet |
Solution 4 | |
Solution 5 | Adarsh Babu PR |