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