If Program If the operation is not performed as an administrator, the system. unauthorizedaccessexception is abnormal when you operate the local file.
To enhance security, the operating systems of Vista and Windows 7 have added the UAC (User Account Control) mechanism. If UAC is enabled, even if the user logs on as administrator, by default, its applications cannot perform write operations on System directories, system registries, and other settings that may affect system operation. This mechanism greatly enhances the security of the system, but for application developers, we cannot force users to close UAC, but sometimes our applications need to run in administrator mode, that is, if Windows 7 runs as administrator, how can we implement this function?
When we run some installation programs in win7, we will find that a dialog box pops up first, asking the user to confirm whether they agree to allow this program to change your computer configuration, however, this prompt is not displayed for the applications we write by default and cannot be run as administrator. This article describes how to set the C # program to prompt users to run with administrator privileges.
First, add an application manifest file to the project.
The default configuration is as follows:
<? 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
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 alization for backward
Compatibility then delete the requestedexecutionlevel node.
-->
< Requestedexecutionlevel Level = "Asinvoker" UIAccess = "False" />
</ Requestedprivileges >
</ Security >
</ Trustinfo >
</ Asmv1: Assembly >
We can see that there is a requestedexecutionlevel item in this configuration, which is used to configure the execution permission level of the current application request. This item has three values to choose from, as shown in the following table:
Value |
Description |
Comment |
Asinvoker |
the application runs with the same access token as the parent process. |
Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document. |
Highestavailable |
The application runs with the highest privileges the current user can obtain. |
Recommended for mixed-mode applications. Plan to refractor the application in a future release. |
Requireadministrator |
The application runs only for administrators and requires that the application must be launched with the full access token of an administrator. |
Recommended for administrator only applications. Internal elevation points are not needed. The application is already running elevated. |
Asinvoker: If this option is selected, the application runs with the current permission.
Highestavailable: this operation is run with the highest permissions available to the current user.
Requireadministrator: this operation only runs as a system administrator.
The default value is asinvoker.
Both highestavailable and requireadministrator prompts you to obtain system administrator privileges. So what are the differences between the two options?
The difference between them is that if we do not log on with an administrator account, if the application is set to requireadministrator, the application will directly run and fail to start. If it is set to highestavailable, the application can run successfully, but it runs with the permissions of the current account rather than the system administrator. If we want the program to run during non-administrator login (some functions should be restricted in this case), we recommend using highestavailable for configuration.
For more information about requestedexecutionlevel settings, see the following link:
Create and embed an application manifest (UAC)
The modified configuration file is as follows:
<? 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
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 alization 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. A prompt box is displayed. Click Yes to continue running the program and obtain the privileges of the system administrator.
Next, let's take a look at how the program knows whether it is currently running as a system administrator or not:
Using system. Security. Principal
Public Static BoolIsadministrator ()
{
Windowsidentity identity = windowsidentity. getcurrent ();
Windowsprincipal principal =NewWindowsprincipal (identity );
ReturnPrincipal. isinrole (windowsbuiltinrole. Administrator );
}
This sectionCodeIt can be used to determine whether the current program runs under the system administrator permission. If asinvoker is configured, false is returned for this function in win7, and true is returned for requireadministrator.
Reference: http://msdn.microsoft.com/zh-cn/library/system.security.principal.windowsidentity.aspx
URL: http://greatverve.cnblogs.com/archive/2011/12/28/win7-admin.html