C # By default run program as administrator to implement code _ practical tips

Source: Internet
Author: User
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.
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.