C # run the program implementation code as an administrator by default

Source: Internet
Author: User

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, so I added the code that runs as Administrator by default in the program. Let's see how it is implemented!

The program runs as an administrator by default.
Copy codeThe Code is as follows:
Static void Main (string [] Args)
{
/**
* When the current user is an administrator, start the application directly.
* If it is not an administrator, use the startup object to start the program to ensure that it 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 ))
{
// Directly 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 an administrator
StartInfo. Verb = "runas ";
// Enable UAC if it is not an administrator
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.

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.