Programming with the System.Deployment.Application Namespace


Programming with the System.Deployment.Application Namespace

In addition to all of the things that ClickOnce automates for you, there are also a lot of things that you can do manually. For instance, you can write code that will manually check for updates, code that initiates the download of an update, or even code that manually downloads an optional file like an image or an unreferenced assembly. You also have control over the updating GUI and can replace the standard update dialog procedure with your own code. All of this is made possible through the System.Deployment.Application namespace and the System.Deployment.Application.ApplicationDeployment class. This class provides the essential functionality that you need to exert maximum control over your application's ClickOnce update environment.

The ApplicationDeployment class contains several events that you can handle within your application. Some of them are

  • CheckForUpdateCompleted This event is fired when an asynchronous check for update availability is complete. Don't confuse this with the completion of an actual download.

  • CheckForUpdateProgressChanged To report progress information back to the user, this event is fired every time the progress of checking for an update changes. It uses the DeploymentProgressChangedEventArgs class to report progress. This class contains a property indicating the percentage completed, but also provides more detail in the form of the BytesCompleted and BytesTotal properties.

  • UpdateCompleted When an asynchronous download and installation of an update is finished, this event is fired to notify the user interface.

  • UpdateProgressChanged This event is fired whenever the progress changes on the download of an update.

To see how you can modify your application to replace the default update behavior with your own, it will take two steps. First, you will have to modify the code to support your new update mechanism and publish that revision out to the publication location. Then, you'll need to make another change so that you can watch your application download the new update.

The first step is to add an Update Now menu item to the form's main menu strip. Also add a progress bar to the status strip on the bottom of the form. If you didn't already have a status strip on your form, now would be a good time to add one. Next, modify your Form1.cs code to look like the code in Listing 39.1.

Listing 39.1. Manually Performing Updates and Reporting Progress

[View full width]

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Deployment.Application; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ClickOnce1 { public partial class Form1 : Form { private ApplicationDeployment ad = null; public Form1() {     InitializeComponent();     ad = ApplicationDeployment.CurrentDeployment;     ad.CheckForUpdateCompleted +=         new CheckForUpdateCompletedEventHandler(ad_CheckForUpdateCompleted);     ad.UpdateProgressChanged +=         new DeploymentProgressChangedEventHandler(ad_UpdateProgressChanged);     ad.CheckForUpdateProgressChanged +=         new DeploymentProgressChangedEventHandler( ad_CheckForUpdateProgressChanged);     ad.UpdateCompleted += new AsyncCompletedEventHandler(ad_UpdateCompleted);     this.Text = "ClickOnce Application (v" + ad.CurrentVersion.ToString() + ")"; } void ad_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) {     tsProgress.Value = e.ProgressPercentage; } void ad_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) {     tsProgress.Value = e.ProgressPercentage; } void ad_UpdateCompleted(object sender, AsyncCompletedEventArgs e) {    if (e.Error != null)    {        MessageBox.Show("There was an error updating the application:\n" +            e.Error.ToString());        return;    }    if (MessageBox.Show("Update installed successfully. Restart application?",        "Application Restart",        MessageBoxButtons.YesNo) == DialogResult.Yes)    {        Application.Restart();    } } void ad_CheckForUpdateCompleted(object sender,   CheckForUpdateCompletedEventArgs e) {     if (e.Error != null)     {         MessageBox.Show("Failed to check for update: \n" + e.ToString());         return;     }     if (e.UpdateAvailable)     {         tsLabel.Text = "Update Found!";         tsProgress.Value = 0;         if (e.IsUpdateRequired)         {             MessageBox.Show("A mandatory update is available. Update will begin  immediately.");             ad.UpdateAsync();         }         else         {             if (MessageBox.Show("An optional update is available. Would you like to  download it now?",                 "Update Available", MessageBoxButtons.YesNo) == DialogResult.Yes)             {                 ad.UpdateAsync();             }         }     }     else     {        tsLabel.Text = "ClickOnce Application";        tsProgress.Value = 0;        MessageBox.Show("No new updates are available for this application.");    } } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {     FormLibrary.AboutBox1 aBox = new FormLibrary.AboutBox1();     aBox.ShowDialog(); } private void updateNowToolStripMenuItem_Click(object sender, EventArgs e) {     // if this application is a clickonce app     if (ApplicationDeployment.IsNetworkDeployed)     {         ad.CheckForUpdateAsync();         tsLabel.Text = "Checking for Updates...";         tsProgress.Value = 0;     } } } } 

Build and then publish this application as version 2.1.0.1, making sure to change the automatic update checking from "Before" to "After." Run the application and make sure that the application updates to the right version. Then, make some minor change to your application and label that version 2.1.0.2. Publish this application and then run the application again. Instead of the default behavior and GUI for updating applications, you should be able to click the Update Now menu item. When checking for updates, you should see the new progress bar at the bottom in the status trip. When an optional update is available, a prompt like the one shown in Figure 39.7 appears. If you choose to download the update, the progress bar again reports progress, but this time it is reporting the download progress of the new update.

Figure 39.7. Custom ClickOnce GUI.


At this point you have created a new application, deployed that application with the click of a button, and created an automatic update framework that takes care of all of the details of downloading new versions, installing those versions, and even rolling them back when failures occur. In addition to that, you have just replaced the default behavior and UI of ClickOnce updating with your own interface and code.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net