C # The program runs as an administrator (three methods)
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;
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tv6hort6wu3qko6pc9wpgo8cd48chjlignsyxnzpq = "brush: java;"> static void Main (string [] Args) {/*** when the current user is an administrator, directly start the application * if it is not an administrator, use the startup object to start the program, to ensure that you use the Administrator identity to run * // get the Windows User ID currently logged on to 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); // 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.
Change
Click "enable ClickOnce Security Settings" in "security" and then re-compile.