C # program to run with administrator privileges
In Vista and Windows 7 and newer operating systems, increasing the UAC (User Account Control) security mechanisms, if UAC is turned on, even if users log in with administrator privileges, their application can not default to the system directory, the system registry, etc. may affect the normal operation of the system set up for writing. This mechanism greatly enhances the security of the system, but the application developers, we can not force users to turn off UAC, but sometimes we have developed applications they need to run as Administrator of the way, how to achieve this function?
The following C # program demonstrates how to achieve prompt users to run with administrator privileges.
In this example program demonstrates WinForm, and modified accordingly a new generation Project:
Method a: initiated by the System.Diagnostics.Process.Start () method:
Method: Review Program Files generated by default, the code is modified as follows:
Having made a comment on the code, not described in detail;
Program static class. 1 2 { . 3 [STAThread] . 4 static void the Main () . 5 { . 6 Application.EnableVisualStyles (); . 7 Application.SetCompatibleTextRenderingDefault (to false); . 8 . 9 / ** 10 * when the current user is an administrator, direct start applications 11 * If you are not an administrator, use the startup Object to start procedures to ensure an administrator to run 12 * / 13 // get the current Windows user logs indicate 14 System.Security.Principal.WindowsIdentity identity = System.Security. Principal.WindowsIdentity.GetCurrent (); 15 System.Security.Principal.WindowsPrincipal new new Principal = System.Security.Principal.WindowsPrincipal (Identity); @ 16 judges whether the current login user is an administrator 29 // set the starting operation, make sure run as administrator. IF. 17 (principal.IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator)) 18 is { . 19 // If an administrator, running directly 20 is the Application.Run (new new the Form1 ()); 21 is} 22 is the else 23 is { 24 / / Create Object start 25 System.Diagnostics.ProcessStartInfo the StartInfo new new System.Diagnostics.ProcessStartInfo = (); 26 is startInfo.UseShellExecute = to true; 27 startInfo.WorkingDirectory = Environment.CurrentDirectory; 28 startInfo.FileName = Application.ExecutablePath; 30 startInfo.Verb = "the runas"; 31 is the try 32 { 33 System.Diagnostics.Process.Start(startInfo); 34 } 35 catch 36 { 37 return; 38 } 39 //退出 40 Application.Exit(); 41 } 42 } 43 }
Effect: Because it is by System.Diagnostics.Process.Start) way of external calls start (so run directly by VS, the VS will not prompt also requires administrator privileges, only the program itself requires administrator privileges, and generate applications different programs. This is the main difference between the two methods and implementation.
This article addresses: http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
Method Two: Listing file by adding applications:
Add New Item to select "application manifest file" in the project and then click the Add button
After the addition, the default file open app.manifest, will:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
change into:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Then open the project properties, will manifest the Applications tab in the resource modification for the new app.manifest.
Rebuild the project, it will prompt you need to run with administrator privileges when you open the program again.
Note that: If you start the debugger in VS, it would prompt this task requires an application with elevated privileges. As shown below:
Choose to use other credentials restart.
Method three: to directly modify file attributes
Right-click the program file, the pop-up Properties dialog box Compatibility tab
Check the "Run this program as administrator" option.
Determine whether the program is running as administrator
We need to add the namespace:
using System.Security.Principal;
/// <summary> /// 确定当前主体是否属于具有指定 Administrator 的 Windows 用户组 /// </summary> /// <returns>如果当前主体是指定的 Administrator 用户组的成员,则为 true;否则为 false。</returns> public static bool IsAdministrator() { bool result; try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); result = principal.IsInRole(WindowsBuiltInRole.Administrator); //http://www.cnblogs.com/Interkey/p/RunAsAdmin.html //AppDomain domain = Thread.GetDomain(); //domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); //WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal; //result = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); } catch { result = false; } return result; }
If you are interested you can continue to view the following link:
http://www.cnblogs.com/Lemon_s/archive/2011/07/28/2119222.html
http://www.cnblogs.com/shenchao/archive/2013/03/05/2944660.html