C # The program runs as an administrator by default.

Source: Internet
Author: User

In the previous blog, I wrote how to update the system time through network time. At that time, I did not test the system successfully. Later I thought about whether it was my operating system (developed on Win8 at that time). At that time, I guess the permission was insufficient and the system time could not be modified. So I ran it once as an administrator and the test was successful! It turned out to be a permission issue.ProgramWhich is run as Administrator by default.Code. Let's see how it is implemented!

 

The program runs as an administrator by default.

 Static   Void Main ( String  [] ARGs ){  /*  ** When the current user is an administrator, directly start the application. * if the user is not an administrator, use the startup object to start the application to ensure that the application runs as an administrator.  */             //  Obtain the Windows User ID for the current Logon System. 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 );  //  Determine whether the current logon user is an administrator              If (Principal. isinrole (system. Security. Principal. windowsbuiltinrole. Administrator )){  //  If it is an administrator, run  Application. enablevisualstyles (); application. Run (  New  Form1 ());}  Else  {  //  Create a startup object System. Diagnostics. processstartinfo startinfo = New System. Diagnostics. processstartinfo ();  //  Set the running File Startinfo. filename = System. Windows. Forms. application. executablepath;  //  Set startup parameters Startinfo. Arguments = string. Join ( "   "  , ArgS );  //  Set the startup action to 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 ();}} 

 

 

Open the program. CS file in the program set and replace the code in the main method with the above Code to run the program as an administrator by default.

This blog was supposed to have been published for a long time. However, due to various reasons such as the network, it has been published until now, and the network problem will soon be solved, the update frequency of each Monday will continue to be restored, and we hope you will continue to pay attention to it.

 

 

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.