C # defaults to running program implementation code as Administrator

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.windows.forms;namespace yyy{Static        The main entry point for the class program {///<summary>//. </summary> [STAThread] static void Main (string[] Args) {/** * Current user             When you are an administrator, start the application directly * if it is not an administrator, start the program with the startup object to ensure that you are running as an administrator *//Get the Windows user ID that is currently logged on            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent ();            Create Windows User topic Application.enablevisualstyles ();            System.Security.Principal.WindowsPrincipal Principal = new System.Security.Principal.WindowsPrincipal (identity); Determines whether the currently logged on user is an administrator if (principal.                 IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator)) {//If it is an administrator, run it directly                Application.enablevisualstyles (); Application.Run (New Form1());  } else {//Create startup object System.Diagnostics.ProcessStartInfo StartInfo = new                System.Diagnostics.ProcessStartInfo ();                Set Run File Startinfo.filename = System.Windows.Forms.Application.ExecutablePath;                Set Startup Parameters startinfo.arguments = String.Join ("", Args);                Set the start action to make sure that run as Administrator Startinfo.verb = "runas";                If it is not an administrator, start UAC System.Diagnostics.Process.Start (startinfo);            Exit System.Windows.Forms.Application.Exit (); }        }    }}

C # defaults to running program implementation code as Administrator

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.