Start→Programs→PhotoViewer→PhotoViewer.
Updating the Application
Let's now update the application so that you can republish the application and see how the changes can be updated on the client side. For simplicity, move the Preview button to the left of the Print from label control as shown in Figure 16-22. This will enable you to verify that the application has been updated after it is republished.

Figure 16-22
To republish the application, simply select Build→Publish PhotoViewer again. When the Publish Wizard appears, click Finish so that it can publish the application using the default settings.
Each time you publish the application, the version number of the application is incremented automatically. That's controlled by the Publish settings page in the project's properties page (see Figure 16-23).

Figure 16-23
In addition, the Publish settings page also contains the Updates button, which enables you to specify how and when the application should check for updates (see Figure 16-24).

Figure 16-24
By default, the application checks for updates every time before it starts.
When the user closes and then relaunches the PhotoViewer application, he gets a prompt, as shown in Figure 16-25.

Figure 16-25
The user can click OK to download the updated application, or click Skip if he doesn't want to update the application now. The updated application will look like Figure 16-26.

Figure 16-26
Programmatically Updating the Application
Instead of the application checking for updates before it starts, it would be a good idea for users to be able to choose when they want to check for updates. For that, add a new button to the form, as shown in Figure 16- 27.

Figure 16-27
Import the following namespace:
using System.Deployment.Application;
Code the Update button like this:
private void btnUpdate_Click(object sender, EventArgs e) {
//---check if the application is deployed by ClickOnce---
if (ApplicationDeployment.IsNetworkDeployed) {
//---Get an instance of the deployment---
ApplicationDeployment deployment =
ApplicationDeployment.CurrentDeployment;
//---if there is any update---
if (deployment.CheckForUpdate()) {
DialogResult response =
MessageBox.Show(('A new version of the ' +
'application is available. ' +
'Do you want to update application?'),
('Application Updates'), MessageBoxButtons.YesNo);
//---if user wants to update---
if (response == DialogResult.Yes) {
Cursor.Current = Cursors.WaitCursor;
//---update the application---
deployment.Update();
//---prompt the user to restart---
MessageBox.Show('Update completed. You need to restart' +
' the application.', ('Update Completed'),
MessageBoxButtons.OK, MessageBoxIcon.Information);
//---restart the application---
Application.Restart();
}
} else {
//---application is up-to-date---
MessageBox.Show(('Application is up-to-date.'), 'Update',
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} else {
//---application is not installed using ClickOnce---
MessageBox.Show(('Application is not installed ' +
'using ClickOnce'), ('Updates not available'),
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
You first check to see if the application is deployed using ClickOnce. This can be done by using the IsNetworkDeployed
property from the ApplicationDeployment
static class. If the application is indeed deployed using ClickOnce
, you proceed to obtain an instance of the deployment using the currentDeployment
property of the ApplicationDeployment
class. Using this instance of the deployment, you call the CheckForUpdate()
method to check whether there is a newer version of the application available from the publishing server. If there is, you prompt the user by asking if he wants to update the application. If he does, you update the application, using the Update() method. After that, you force the user to restart the application, using the Restart()
method.
To test the update, first run an instance of the PhotoViewer application by launching it from the Start menu. Next, republish the application in Visual Studio 2008. Click the Update button to see if an update is available. You should see the prompt shown in Figure 16-28. Click Yes, and the application will be updated.

Figure 16-28
Rolling Back