ClickOnce administrator starts and supports ClickOnce updates

Source: Internet
Author: User

ClickOnce administrator starts and supports ClickOnce updates

Development requirements: WPF Apps publish apps through ClickOnce and must launch apps with administrator privileges, providing manual detection of updates.

When it comes to WPF development, we know that using ClickOnce to package and publish an app makes it easy to apply update management, and ClickOnce can use the update function as soon as it is packaged with the update policy set. But with administrator privileges so it won't work, what's the solution?

After a lot of data query, finally in a post found a solution.

First, let's take a look at the code that starts with the administrator. The following code is called in the Onstartup method of application to start the ClickOnce app with administrator privileges.

private void CheckAdministrator(){    var wi = WindowsIdentity.GetCurrent();    var wp = new WindowsPrincipal(wi);    bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);    if (!runAsAdmin)    {        var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)        {            UseShellExecute = true,            Verb = "runas",            Arguments = StartFlag        };        try        {            Process.Start(processInfo);        }        catch (Exception ex)        {}        Environment.Exit(0);    }}

But why does the ClickOnce update function fail?

See assembly.getexecutingassembly (). CodeBase this code, actually get the EXE file address of the ClickOnce application, so that directly start EXE will cause applicationdeployment.isnetworkdeployed=false, The ClickOnce update function fails because the application is not a ClickOnce application.

Next, resolve the issue of the update failure. According to the above blog, after the use of administrator rights to start the application, and then through the Appref-ms boot can be both Administrator privileges and ClickOnce update functionality.

In fact, the app will start three times, the first time the user clicks the shortcut into the application, the administrator rights to start the application (that is, the second boot), see the Checkadministrator code in the ProcessInfo parameter, where arguments set a flag logo, After the second boot, because args in the StartupEventArgs parameter contains the flag ID, the third boot is done through Appref-ms and the current process exits.

On top of that, the third launch app combines both administrator privileges and ClickOnce functionality.

private void App_Startup(object sender, StartupEventArgs e){    if (e.Args != null && e.Args.Length > 0 && (e.Args[0].Equals(StartFlag)))    {        var flag = e.Args[0];        if(StartFlag.Equals(flag))        {            StartAppAgain();        }        Environment.Exit(0);    }    else    {        CheckAdministrator();    }}

Through the Appref-ms boot code as follows, the focus is how to get appref-ms path.

private void StartAppAgain(){    string publisherName = System.Windows.Forms.Application.CompanyName;    string productName = System.Windows.Forms.Application.ProductName;    string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);    string shortcutPath = Path.Combine(allProgramsPath, publisherName);    shortcutPath = Path.Combine(shortcutPath, productName) + ".appref-ms";    try    {        Process.Start(shortcutPath);    }    catch (Exception ex)    {}}

ClickOnce administrator starts and supports ClickOnce updates

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.