C # enables programs to run as administrators through app.manifest

Source: Internet
Author: User

Typically, programs written in C # do not eject this hint and will not run as an administrator. Microsoft's operating system using Microsoft's product approach, of course, there are, through the app.manifest configuration can make the program open, the pop-up UAC prompts need to be allowed to continue, so that the administrator has the authority to execute the program

Microsoft has introduced a new technology for UAC (user Account Control) in Windows Vista (click here to learn what UAC is). When the program executes with permissions, UAC pops up a warning message: Let the user confirm that they agree to allow the program to change your computer configuration, and Windows needs your permission to keep the program going.

Typically, programs written in C # do not eject this hint and will not run as an administrator. Microsoft's operating system uses Microsoft's product approach, of course, there are, through the app.manifest configuration can make the program open, the pop-up UAC prompts need to be allowed to continue, so that the administrator's permission to execute the program.

Here's how:

Open vs2005, vs2008 and other projects, in Solution Explorer to see if there is app.manifest this file, if not, by the following methods to create:

Method 1:

1, in the "Solution Explorer" right-click Project Name Selection properties;
2. In the dialog box that pops up, select the Security tab.
3. Tick "Enable ClickOnce Security Settings" and select "This is a fully trusted application".
4, Save the project, this time app.manifest is automatically created.




Open App.manifest to modify the default configuration, through the code comments, you can know that the node that needs to be modified is "requestedExecutionLevel"





AsInvoker: If you choose this, the application is running with the current permissions.

Highestavailable: This is run with the highest privileges available to the current user.

Requireadministrator: This is only run with system administrator privileges.

Find <requestedexecutionlevel level= "AsInvoker" uiaccess= "false"/>
Change it to <requestedexecutionlevel level= "Requireadministrator" uiaccess= "false"/>

At this point, when the modification is complete, press F5 for debugging, you will usually receive the following error: "ClickOnce does not support request execution level" Requireadministrator "".

It seems that there is a contradiction between the above, "ClickOnce" on the automatic creation of the app.manifest, and now modify the app.manifest prompt: ClickOnce does not support the request execution level "Requireadministrator".

The workaround is to simply uncheck "Enable ClickOnce Security Settings".

If the procedure is correct, press F5 for debugging, the following prompt will pop up, VS has elevated privileges.



In this way, when the application we write is run, it will first pop up such a prompt, click "Yes", the program can continue to run, and get the privileges of the system administrator.

Method 2:

Add a class to the project, application manifest file (app.manifest).




You can then see that the location of the app.manifest is not in the properties attribute, it becomes in the reference, and its node content is less.


The rest of the method is the same as above. This skips the "Enable ClickOnce security Settings" step and executes directly without error.

C # enables programs to run as administrators through app.manifest

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.