WinForm Program Plus Administrator privileges

Source: Internet
Author: User

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;

        Static classProgram {[STAThread]Static voidMain () {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); /** * When the current user is an administrator, start the application directly * if it is not an administrator, use the Startup Object Launcher to ensure that you are running with Administrator*/                //get the currently logged in Windows User logoSystem.Security.Principal.WindowsIdentity identity =System.Security.Principal.WindowsIdentity.GetCurrent (); System.Security.Principal.WindowsPrincipal Principal=NewSystem.Security.Principal.WindowsPrincipal (identity); //determine whether the currently logged on user is an administrator                if(Principal. IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator)) {//If it is an administrator, run it directlyApplication.Run (NewForm1 ()); }                Else                {                    //To Create a startup objectSystem.Diagnostics.ProcessStartInfo StartInfo =NewSystem.Diagnostics.ProcessStartInfo (); Startinfo.useshellexecute=true; Startinfo.workingdirectory=environment.currentdirectory; Startinfo.filename=Application.executablepath; //set the start action to ensure that you are running as an administratorStartinfo.verb ="runas"; Try{System.Diagnostics.Process.Start (startinfo); }                    Catch                    {                        return; }                    //ExitApplication.exit (); }            }        }    

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.

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.

WinForm Program Plus Administrator privileges

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.