On-Demand Updates with ClickOnce
ClickOnce supports both automatic updating and on-demand updates. The default model checks for updates automatically at application launch (when connected), and applies those updates immediately. There are a number of options available for installed applications (available online and offline), including whether to check automatically at all, whether to do it on a background thread, whether to do it on a timed interval, and other options.
However, sometimes you may want to do updates on demand, either as the only update model, or in combination with automatic updating in the background on a periodic interval. To do that, you will need to use the ClickOnce API defined in the System.Deployment framework assembly.
ClickOnce supports both automatic updating and on-demand updates
The main class you will use to support on-demand updates is the ApplicationDeployment-class defined in System.Deployment.Application. You will typically add code in response to a user action (such as selecting a Check For Updates menu-item) that goes out and checks to see if updates are available, and if so retrieves them. You will then need to restart the application to have those changes applied.
A simple but common pattern to accomplish this is something like the following:
' First check to see if we are running in a ClickOnce
' context
If ApplicationDeployment.IsNetworkDeployed Then
' Get an instance of the deployment
Dim deployment As ApplicationDeployment = _
ApplicationDeployment.CurrentDeployment
' Check to see if updates are available
If deployment.CheckForUpdate() Then
Dim res As DialogResult = MessageBox.Show(_
"A new version of the application is available,_
do you want to update?", _
"Application Updater", MessageBoxButtons.YesNo)
If res = DialogResult.Yes Then
' Do the update
deployment.Update()
Dim res2 As DialogResult = MessageBox.Show(_
"Update complete, do you want to restart the_
application to apply the update?",_
"Application Updater", MessageBoxButtons.YesNo)
If res2 = DialogResult.Yes Then
' Restart the application to apply the update
Application.Restart()
End If
End If
Else
MessageBox.Show("No updates available.",_
"Application Updater")
End If
Else
MessageBox.Show("Updates not allowed unless you are_
launched through ClickOnce.")
End If
There are of course asynchronous versions of the CheckForUpdate- and Update-methods if you want to avoid blocking your UI while this happens.
The project settings you will need may not be completely apparent
The project settings you will need may not be completely apparent. The first important setting is that you need to change the update-behavior of the ClickOnce-deployment to stop automatically checking for updates if you are doing only on-demand updates. You do this through the Publish-tab of the project properties settings, Updates button, shown in figure 1.

Fig. 1: ClickOnce Update Behavior Settings access
When you click that button, the dialog in figure 2 will show with default settings selected as shown.

Fig. 2: ClickOnce Update Settings Dialog
What you will want to do for a pure on-demand updates application is to uncheck the box that says the application should check for updates at the top. The other trick that is not apparent but required is that you have to specify an Update-location at the bottom or you will get an obscure error message when you try to launch the application on the client. So you should set up the update settings like shown in figure 3.

Fig. 3: ClickOnce On-Demand Update Settings
With those settings in place, when you publish your application out and the client launches it, they can invoke the code shown earlier to check for and apply updates on-demand.
Editorial note: For those who are currently using VS 2005 Beta 2 (or earlier), there is a bug in the ClickOnce on-demand update system. The bug has to do with an IllegalOperationException you get when calling CheckForUpdate(). This bug is fixed in the RC1 version of Visual Studio 2005, but there is a work-around. The workaround in Beta 2 is to tell the app to check for updates, set it to do so after the application starts, and set the subscription period (how often to check for updates) to a long value, such as 50 weeks. Be careful, because another bug is that VS will let you set an illegally large value for this subsciption, such as 512 weeks. If you do so though, you will get a deployment error because ClickOnce will see a schema violation, and if you try to edit the manifest with MaGe, it will crash with an unhandled exception. You can read more about this bug on the blog of Brian Noyes, go to http://www.softinsight.com/bnoyes/PermaLink.aspx?guid=896b807d-37a9-4ac1-a6d2-5bacc28d9963 for the article about this.
If you wanted to combine on-demand updates with periodic background checking for updates, you can do that by leaving updates enabled, but you will need to select the option to check for updates after the application starts. You will then want to configure the frequency of checking using the options in the middle of the dialog.
De sources die bij dit artikel horen kunt u downloaden via Noyes_OnDemandUpdatesWithClickOnce_SRC.zip.