web 2.0

Programmatically Update ClickOnce Applications

Hi,

In this article I'll talk about ClickOnce Deployed Application and Update Process.

In normal usage Update Checking is performed when application start, but often we need to check update after application started.

MSDN : ApplicationDeployment Class

ApplicationDeployment Class

Updated: July 2008

Supports updates of the current deployment programmatically, and handles on-demand downloading of files. This class cannot be inherited.

 

You can configure your ClickOnce application to check for updates and install them automatically through the subscription element of the deployment manifest. Some applications, however, need finer control over their updates. You may want to install required updates programmatically, and prompt users to install optional updates at their convenience. By turning off subscription updates in the deployment manifest, you can take complete control of your application's update policies. Alternatively, you can use automatic subscription in conjunction with ApplicationDeployment, which enables ClickOnce to update the application periodically, but uses ApplicationDeployment to download critical updates shortly after they are released.

You can test whether your deployment has an available update by using either the CheckForUpdate or the CheckForUpdateAsync method; the latter method raises the CheckForUpdateCompleted event on successful completion. CheckForDetailedUpdate returns important information about the update, such as its version number and whether it is a required update for current users. If an update is available, you can install it by using Update or UpdateAsync; the latter method raises the UpdateCompleted event after installation of the update is complete. For large updates, you can receive progress notifications through theCheckForUpdateProgressChanged and UpdateProgressChanged events, and use the information in ProgressChangedEventArgs to notify the user of the download status.

 

So here is how to make it working.

Standard ClickOnce Update

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Deployment.Application;

namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                Boolean IsUpToDate = ApplicationDeployment.CurrentDeployment.CheckForUpdate();
                if (!IsUpToDate)
                {
                    try
                    {
                        // Maybe Save Current Application State

                        // Start Update
                        ApplicationDeployment.CurrentDeployment.Update();
                    }
                    catch { MessageBox.Show("Error while Updating Application"); }
                    finally
                    {
                        Application.Restart(); // Restart the Application
                    }
                }
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Async ClickOnce Update

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Deployment.Application;
using System.ComponentModel;

namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                Boolean IsUpToDate = ApplicationDeployment.CurrentDeployment.CheckForUpdate();
                if (!IsUpToDate)
                {
                    // Maybe Save Current Application State

                    ApplicationDeployment.CurrentDeployment.UpdateCompleted
                        += new AsyncCompletedEventHandler(UpdateCompleted);

                    // Start Update Asynchronously
                    ApplicationDeployment.CurrentDeployment.UpdateAsync();
                }
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private static void UpdateCompleted(object sender, AsyncCompletedEventArgs e)
        {
            Application.Restart(); // When Completed : Restart the Application
        }
    }
}

Hope this help's!



Views(2445)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | Windows

Comments

Technorati Profile