C # runs program implementation code as Administrator by default

Source: Internet
Author: User
Tags exit continue

  Permissions are not enough, resulting in the inability to modify the system time, so I ran as an administrator, the results of the test success, here is a description of how C # by default as an administrator to run programs

Last blog wrote about how to update the system time through the network time, when writing how to test is not successful, and then think of my operating system (then was developed on the Win8) problem. At that time I guess should be insufficient permissions, resulting in the inability to modify the system time, so I ran as an administrator, the results of the test success! Originally really is the question of permissions, so in the program to add the default as an administrator to run the code. Let's take a look at how to achieve it!     programs run as admin by default   code: static void Main (string[] Args)   { /**  * When the current user is an administrator, start the application directly &nbs P * If not an administrator, start the program using the startup object to make sure that you are running   */ //Getting the currently logged on Windows user ID   System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent ();  //Create Windows user theme   Application.enablevisualstyles ();    System.Security.Principal.WindowsPrincipal Principal = new System.Security.Principal.WindowsPrincipal (identity);  //Determine if the current logged-on user is an administrator   if (principal). IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator))   { ///If administrator, run 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 up startup action to ensure running as Administrator   Startinfo.verb = " runas "; //If not an administrator, start uac  System.Diagnostics.Process.Start (startinfo); //Exit   System.Windows.Forms.Application.Exit (); } }    Open the Program.cs file in the assembly and replace the code in the main method with the above code to enable the program to run as an administrator by default.     This blog should have been published long ago, but because of the network and other reasons, until now to be published, network problems will soon be resolved, will continue to revert to a weekly update frequency, I hope we continue to pay attention to.  

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.