C # defaults to running programs as an administrator

Source: Internet
Author: User

The previous blog wrote about how to update the system time through the network time, when writing how the test was unsuccessful, and then think about whether my operating system (at that time was developed on the Win8) problem. At that time I guess it should be insufficient authority, resulting in the inability to modify the system time, so I ran as an administrator, the results test success! Originally really is the problem of permissions, so in the program is added to the default as an administrator to run the code. Let's see how it's going to come true!

Program runs as Administrator by default

Staticvoid Main (String[] Args) {/** * When the current user is an administrator, start the application directly * if it is not an administrator, use the Startup Object Launcher to ensure that you are running with administrator*///Get the currently logged on Windows user flag System.Security.Principal.WindowsIdentity identity =System.Security.Principal.WindowsIdentity.GetCurrent ();//Create a Windows user topicApplication.enablevisualstyles (); System.Security.Principal.WindowsPrincipal Principal =NewSystem.Security.Principal.WindowsPrincipal (identity);//Determine whether the currently logged on user is an administratorIf(Principal. IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator)) {//If it is an administrator, run it directlyApplication.enablevisualstyles (); Application.Run (NewForm1 ()); }Else{//Create a Startup object System.Diagnostics.ProcessStartInfo StartInfo =NewSystem.Diagnostics.ProcessStartInfo ();// set Run file Startinfo.filename = System.Windows.Forms.Application.ExecutablePath; // set startup parameters startinfo.arguments = String.Join ("  "// Set start action, make sure to run 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 code above to enable the program to run as administrator by default.

This blog originally should have been published, but because of the network and other reasons, until now to publish, network problems will soon be resolved, will continue to restore to a weekly update frequency, I hope you continue to pay attention.

C # defaults to running programs as an administrator

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.