C # The program runs as an administrator (three methods)

Source: Internet
Author: User

Three methods:

1. Set Properties of the program: Check "run this program as administrator", and set "change all user settings-run this program as administrator" if necessary ", of course, this method is passive and not the most practical one;



II,Code method:

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 program, to ensure that you use the Administrator identity to run * // obtain the Windows User ID system currently logged on. 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); // determines whether the current logon user is an administrator if (principal. isinrole (system. security. principal. windowsbuiltinrole. administrator) {// if it is an administrator, run application directly. enablevisualstyles (); application. run (New form1 ();} else {// create the startup object system. diagnostics. processstartinfo startinfo = new system. diagnostics. processstartinfo (); // sets the running file startinfo. filename = system. windows. forms. application. executablepath; // set the startup parameter // startinfo. arguments = string. join ("", argS); // sets the startup action to ensure that startinfo is run as an administrator. verb = "RunAs"; try {// if not the administrator, start UAC system. diagnostics. process. start (startinfo); // exit system. windows. forms. application. exit () ;}catch {}}}

III, Configuration method:

In vs2010, right-click the project property-security-enable clickonce Security Settings
 
Then, an app. manifest file is generated in the program directory.
 
<Requestedexecutionlevel level = "asinvoker" UIAccess = "false"/>
Change
<Requestedexecutionlevel level = "requireadministrator" UIAccess = "false"/>

Click "enable clickonce Security Settings" in "security" and then re-compile.



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.