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!
Program runs as Administrator by default
Copy Code code as follows:
static void Main (string[] Args)
{
/**
* Start the application directly when the current user is the administrator
* If not an administrator, start the program using the startup object to ensure that you are running as an administrator
*/
Get the currently logged on Windows user logo
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent ();
Create a Windows user theme
Application.enablevisualstyles ();
System.Security.Principal.WindowsPrincipal Principal = new System.Security.Principal.WindowsPrincipal (identity);
Determine if the currently logged-on user is an administrator
if (principal. IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator))
{
If you are an administrator, run directly
Application.enablevisualstyles ();
Application.Run (New Form1 ());
}
Else
{
Create a Startup Object
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo ();
Set up run files
Startinfo.filename = System.Windows.Forms.Application.ExecutablePath;
Setting Startup Parameters
Startinfo.arguments = String.Join ("", Args);
Set the startup action to ensure that you are running as an 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 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.