VS to the generated program (EXE) by adding the default request Administrator permissions

Source: Internet
Author: User

In the VS programming, often use some special permissions, especially the administrator permissions, the following is the Win7 under the author's own test passed.

Here are two things to do:

The first case is to run in the vs2003 or the following compiler

1, prepare a manifest file, named as follows: Xpstyle.manifest

The contents of the file are as follows:

<?XML version= "1.0" encoding= "UTF-8" standalone= "yes"?> <Assemblyxmlns= "Urn:schemas-microsoft-com:asm.v1"manifestversion= "1.0"> <assemblyidentityversion= "5.1.0.9"processorarchitecture= "X86"name= "Xpstyle.manifest"type= "Win32" /> <TrustInfoxmlns= "Urn:schemas-microsoft-com:asm.v3">   <Security>     <requestedprivileges>         <requestedExecutionLevel Level= "Requireadministrator"uiAccess= "false"/>     </requestedprivileges>   </Security> </TrustInfo> </Assembly>

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.

2. Then save, put in and the resource file (. rc) in a directory

3. Open the above resource file with the file editor and add the last line

1 "Xpstyle.manifest"

1 is the resource id,24 is the name of the resource and must be 1 and 24

4, and then save the recompile program just fine.

Here is a note to explain: This is the generation of EXE files can not debug run, only double-click to run, to use the administrator's debugging, you need to let the compiler run as an administrator at the same time the resource file added to the line comment out.

If the direct point debugging operation will cause you to shut down the program, with the task manager can not be turned off.

The second case is running in the compiler above VS2005

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.

VS to the generated program (EXE) by adding the default request Administrator permissions

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.