WPF runs as an administrator after being published as Clickonce. wpfclickonce
WPF programs are released in Clickonce mode. After Windows 7 is installed, it is found that some operations may cause program exceptions. After troubleshooting, it is found that the permission problem occurs.
Is an exception thrown when File. Move is executed: access to the path is denied.
The first thing that comes to mind is: Right-click --> a program that runs as an administrator but has been published by Clickonce. This option is not available in the right-click menu.
So how can I run as an administrator?
According to the search answer,
1) Right-click Project Properties
2) Select security and select "enable ClickOnce Security Settings"
3) In the Properties folder of the project, find app. mainfest
4) change the node
<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>
Change
<RequestedExecutionLevel level = "requireAdministrator" uiAccess = "false"/>
5) return to the security option in the Project Properties and remove "enable ClickOnce Security Settings ".
6) Save and compile
Till now, it's still smooth,
7) release... Error!
Error 6: ClickOnce does not support the request execution level 'requireadministrator '.
What is this shit? When you continue searching, the search results indicate that you want to go to the project properties page. On the Security tab, select "enable ClickOnce Security Settings.
Then, remove the check box and re-release it. An error is returned. On the Security tab, you can find that "enable ClickOnce Security Settings" is selected again.
Strange .....
After searching for N for a long time, I finally found a solution.
In order to allow the released program to gain administrator privileges
1) in the app. mainfest File
<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>
Set the node value to asInvoker.
2) Add the following code to App. cs:
/// <Summary> /// check whether the Administrator identity is correct /// </summary> private void CheckAdministrator () {var wi = WindowsIdentity. getCurrent (); var wp = new WindowsPrincipal (wi); bool runAsAdmin = wp. isInRole (WindowsBuiltInRole. administrator); if (! RunAsAdmin) {// It is not possible to launch a ClickOnce app as administrator directly, // so instead we launch the app as administrator in a new process. var processInfo = new ProcessStartInfo (Assembly. getExecutingAssembly (). codeBase); // The following properties run the new process as administrator processInfo. useShellExecute = true; processInfo. verb = "runas"; // Start the new process try {Process. start (processInfo);} catch (Exception ex) {ex. writeLog ();} // Shut down the current process Environment. exit (0 );}}
3) rewrite the OnStartup function.
Protected override void OnStartup (StartupEventArgs e) {base. OnStartup (e); CheckAdministrator (); // if it is not an administrator, the program exits and runs again as an administrator. StartupUri = new Uri ("MainWindow. xaml", UriKind. RelativeOrAbsolute );}
4) Save, regenerate, and publish
After adding the code above, release and install it again using ClickOnce. When running, the dialog box "do you want to allow the following programs from unknown publishers to change this computer" is displayed. Click "yes" to run the program as an administrator.
Reference: http://www.codeproject.com/Tips/627850/ClickOnce-deployment-vs-requestedExecutionLevel-eq
How does WPF run as an administrator by default?
From: www.cnblogs.com/...5.html
/**
* 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
... The remaining full text>
ClickOnce release program
Change your release location to local to check whether the path is correct.