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.