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.