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
- <? XML version= "1.0" encoding="Utf-8"?>
- <asmv1:assembly manifestversion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1 "
- xmlns:asmv1= "urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="Urn:schemas-microsoft-com:asm.v2 "
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <assemblyidentity version="1.0.0.0" name="Myapplication.app"/>
- <trustinfo xmlns="Urn:schemas-microsoft-com:asm.v2">
- <security>
- <requestedprivileges xmlns="Urn:schemas-microsoft-com:asm.v3">
- <!--UAC Manifest Options
- If you want to change the Windows User account Control level replace the
- requestedExecutionLevel node with one of the following.
- <requestedexecutionlevel level="AsInvoker" uiaccess="false" />
- <requestedexecutionlevel level="requireadministrator" uiaccess="false" />
- <requestedexecutionlevel level="highestavailable" uiaccess="false" />
- If you want to utilize File and Registry virtualization for backward
- Compatibility then delete the requestedExecutionLevel node.
- -->
- <requestedexecutionlevel level="requireadministrator" uiaccess="false" />
- </requestedprivileges>
- </Security>
- </trustinfo>
- </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
- Public static bool Isadministrator ()
- {
- WindowsIdentity identity = WindowsIdentity.GetCurrent ();
- WindowsPrincipal principal = new WindowsPrincipal (identity);
- return principal. IsInRole (Windowsbuiltinrole.administrator);
- }
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)