ApplicationDeployment.CheckForUpdateAsync Method
Assembly: System.Deployment (in system.deployment.dll)
CheckForUpdateAsync will return immediately and start a separate thread for downloading and installing the updated application. When the method completes or is canceled, it will raise the CheckForUpdateCompleted event. You can use AsyncCompletedEventArgs in this event handler to determine whether the operation was canceled, or if an exception was raised during update.
The following example checks for an update asynchronously, and installs the update if one exists.
long sizeOfUpdate = 0; private void UpdateApplication() { if (ApplicationDeployment.IsNetworkDeployed) { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; ad.CheckForUpdateCompleted += new CheckForUpdateCompletedEventHandler(ad_CheckForUpdateCompleted); ad.CheckForUpdateProgressChanged += new DeploymentProgressChangedEventHandler(ad_CheckForUpdateProgressChanged); ad.CheckForUpdateAsync(); } } void ad_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) { downloadStatus.Text = String.Format("Downloading: {0}. {1:D}K of {2:D}K downloaded.", GetProgressString(e.State), e.BytesCompleted/1024, e.BytesTotal/1024); } string GetProgressString(DeploymentProgressState state) { if (state == DeploymentProgressState.DownloadingApplicationFiles) { return "application files"; } else if (state == DeploymentProgressState.DownloadingApplicationInformation) { return "application manifest"; } else { return "deployment manifest"; } } void ad_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("ERROR: Could not retrieve new version of the application. Reason: \n" + e.Error.Message + "\nPlease report this error to the system administrator."); return; } else if (e.Cancelled == true) { MessageBox.Show("The update was cancelled."); } // Ask the user if they would like to update the application now. if (e.UpdateAvailable) { sizeOfUpdate = e.UpdateSizeBytes; if (!e.IsUpdateRequired) { DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?\n\nEstimated Download Time: ", "Update Available", MessageBoxButtons.OKCancel); if (DialogResult.OK == dr) { BeginUpdate(); } } else { MessageBox.Show("A mandatory update is available for your application. We will install the update now, after which we will save all of your in-progress data and restart your application."); BeginUpdate(); } } } private void BeginUpdate() { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; ad.UpdateCompleted += new AsyncCompletedEventHandler(ad_UpdateCompleted); // Indicate progress in the application's status bar. ad.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(ad_UpdateProgressChanged); } void ad_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) { String progressText = String.Format("{0:D}K out of {1:D}K downloaded - {2:D}% complete", e.BytesCompleted / 1024, e.BytesTotal / 1024, e.ProgressPercentage); downloadStatus.Text = progressText; } void ad_UpdateCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("The update of the application's latest version was cancelled."); return; } else if (e.Error != null) { MessageBox.Show("ERROR: Could not install the latest version of the application. Reason: \n" + e.Error.Message + "\nPlease report this error to the system administrator."); return; } DialogResult dr = MessageBox.Show("The application has been updated. Restart? (If you do not restart now, the new version will not take effect until after you quit and launch the application again.)", "Restart Application", MessageBoxButtons.OKCancel); if (DialogResult.OK == dr) { Application.Restart(); } }
- PermissionSet For full access to the local computer. Associated enumeration: PermissionState
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
tfl - from the Italian:
If you wish to find code and the explanation in Italian can visit the site noted bove. Also, for a machine translation of the page, see http://babelfish.yahoo.com/translate_url?doit=done&tt=url&intl=1&fr=bf-res&trurl=http%3A%2F%2Fwww.vbdotnet.it%2Fpost%2F2008%2F08%2F05%2FCreare-un-LiveUpdate-dalle-proprie-applicazioni.aspx&lp=it_en&btnTrUrl=Translate) or see http://tinyurl.com/5gqm6f.
- 8/4/2008
- Enrico Rossini
- 8/5/2008
- Thomas Lee
- 6/14/2006
- irhetoric
- 8/5/2008
- Thomas Lee