C # The program implementation code is run as the administrator by default,

Source: Internet
Author: User

C # The program implementation code is run as the administrator by default,

Using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace yyy {static class Program {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main (string [] Args) {/*** start the application directly when the current user is an administrator * if not, start the program using the startup object to ensure that the System is running as an administrator * // obtain the Windows User ID currently logged on. security. principal. windowsIdentity identity = System. security. principal. windowsIdentity. getCurrent (); // create a Windows user topic Application. enableVisualStyles (); System. security. principal. windowsPrincipal principal = new System. security. principal. windowsPrincipal (identity); // determines whether the current logon user is an administrator if (principal. isInRole (System. security. principal. windowsBuiltInRole. administrator) {// if it is an Administrator, run Application directly. enableVisualStyles (); Application. run (new Form1 ();} else {// create the startup object System. diagnostics. processStartInfo startInfo = new System. diagnostics. processStartInfo (); // sets the running file startInfo. fileName = System. windows. forms. application. executablePath; // set the startup parameter startInfo. arguments = String. join ("", Args); // sets the startup action to ensure that startInfo is run as an administrator. verb = "runas"; // if it is not an administrator, start UAC System. diagnostics. process. start (startInfo); // exit System. windows. forms. application. exit ();}}}}

 

Related Article

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.