VS2005, introducing app.manifest in 2008,2010 (that is, C # programs run with Administrator privileges under Win7)

Source: Internet
Author: User

Open VS2005, VS2008, VS2010 project, see if there is a app.manifest file under the Properties folder in the project folder; if not, create it as follows: Right-click the project Select "Properties" from the menu and click on "Security" of the project properties. tab, check "Enable ClickOnce Security Settings" On the Security tab and select "This is a fully trusted application" to save the project, where the App.manifest file has been automatically generated under properties.

Change the default app.manifest file to

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <asmv1:assembly manifestversion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1 "
  3. xmlns:asmv1= "urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="Urn:schemas-microsoft-com:asm.v2 "
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <assemblyidentity version="1.0.0.0" name="Myapplication.app"/>
  6. <trustinfo xmlns="Urn:schemas-microsoft-com:asm.v2">
  7. <security>
  8. <requestedprivileges xmlns="Urn:schemas-microsoft-com:asm.v3">
  9. <!--UAC Manifest Options
  10. If you want to change the Windows User account Control level replace the
  11. requestedExecutionLevel node with one of the following.
  12. <requestedexecutionlevel level="AsInvoker" uiaccess="false" />
  13. <requestedexecutionlevel level="requireadministrator" uiaccess="false" />
  14. <requestedexecutionlevel level="highestavailable" uiaccess="false" />
  15. If you want to utilize File and Registry virtualization for backward
  16. Compatibility then delete the requestedExecutionLevel node.
  17. -->
  18. <requestedexecutionlevel level="requireadministrator" uiaccess="false" />
  19. </requestedprivileges>
  20. </Security>
  21. </trustinfo>
  22. </asmv1:assembly>

After the configuration file is modified, we run the application, we will first pop up such a prompt box, click Yes, the program can continue to run.

By the way, there is also a way to find out if the program is running as Administrator right at this point:

[CSharp]View PlainCopy
    1. Public static bool Isadministrator ()
    2. {
    3. WindowsIdentity identity = WindowsIdentity.GetCurrent ();
    4. WindowsPrincipal principal = new WindowsPrincipal (identity);
    5. return principal.  IsInRole (Windowsbuiltinrole.administrator);
    6. }


The UAC execution permission level referenced in the XML file represents the following meanings, respectively:

AsInvoker: 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.

By default, it is AsInvoker.

Both the highestavailable and Requireadministrator options can prompt the user for system administrator privileges. So what's the difference between these two options?

The difference is that if we are not logged in as an administrator account, then if the application is set to Requireadministrator, then the application will fail to run directly and cannot start. If set to Highestavailable, the application can run successfully, but runs with the permissions of the current account instead of system administrator privileges. If we want the program to run when a non-admin account is logged in (in which case some functionality should be limited), we recommend that you configure it with highestavailable.

VS2005, introducing app.manifest in 2008,2010 (that is, C # programs run with Administrator privileges under Win7)

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.