C # programs run with Administrator privileges
In Vista and Windows 7 and later versions of the operating system, the security mechanism for UAC (user Account Control) has been increased, and if UAC is turned on, the user will not be able to log on to the system directory by default, even with administrator privileges. The system registry, and other settings that may affect the system's normal operation are written. This mechanism greatly enhances the security of the system, but for the application developer, we cannot force the user to turn off UAC, but sometimes the applications we develop need to be run in a Administrator way, how do we implement such a function?
The following shows how the C # program implements prompting the user to run with administrator privileges.
In this example, the WinForm program demonstrates that a new project is created and then modified accordingly:
Method One: Start with the System.Diagnostics.Process.Start () mode:
Implementation method: Modify the default generated program file, the modified code is as follows:
Since comments have been made on the code, they are no longer described in detail;
1 Static Class Program 2 {3 [STAThread] 4 static void Main () 5 {6 Application.enablevisualstyles (); 7 Application.setcompatibletextrenderingdefault (FALSE); 8 9/**10 * When the current user is an administrator, start the application directly 11 * If it is not an administrator, start the program with the startup object to ensure that you are running with Administrator 12 */13//Get the current logged in Windows user flag System.Security.Principal.WindowsIdentity identity = System.Security . Principal.WindowsIdentity.GetCurrent (); System.Security.Principal.WindowsPrincipal Principal = new system.se Curity. Principal.windowsprincipal (identity); 16//Determine if the current logged on user is an administrator (Principal. IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator)) 18 {19//If you are an administrator, run 20 directly Application.Run (New Form1 ());}22 Else23 {24//Create Startup Object System.Diagnostics.ProcessStartInfo STARtinfo = new System.Diagnostics.ProcessStartInfo (); startinfo.useshellexecute = true;27 Startinfo.workingdirectory = environment.currentdirectory;28 Startinfo.filename = Application.ExecutablePa TH;29//Set Start action, make sure to run as Administrator Startinfo.verb = "runas"; try32 {System.Diagnostics.Process.Start (startinfo);}35 catch36 {PNS return;38}39//Exit Application.exit (); 41}42}43}
Effect: Because it is started by an external call via the System.Diagnostics.Process.Start (), the VS runtime is not prompted for the VS also requires Administrator privileges, and only the program itself requires administrator privileges, unlike the program that generates the application. This is the main difference from the implementation of the method two.
This address: http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
Method two: By adding the application manifest file:
Add a new item on the project select application manifest file and click the Add button
When added, the App.manifest file is opened by default and will:
<requestedexecutionlevel level= "AsInvoker" uiaccess= "false"/>
Modified to:
<requestedexecutionlevel level= "Requireadministrator" uiaccess= "false"/>
Then open the project properties to modify the manifest in the resources in the Application tab to the new app.manifest.
Rebuild the project, and you will be prompted to run with administrator privileges when you open the program again.
It is important to note that if you start debugging in VS, you will be prompted to ask the application for elevated permissions. Such as:
Choose to restart with additional credentials.
Method Three: Directly modify the properties of the program file
Right-click the program file in the Compatibility tab of the Pop-up Properties dialog box
Tick "Run this program as Administrator".
form:http://blog.csdn.net/huwei2003/article/details/24235367
"Go" C # programs run with Administrator privileges