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