Under certain circumstances, you may not want an app to automagically update itself. This slab shows how to do it manually.
When and where you can, you should use the automatic application update for ClickOnce published apps. To do this:
- Double click My Project in the Solution Explorer.
- Navigate to the Publish tab.
- Click the Updates… button.
- Check “The application should check for updates”.
- Check “Before the application starts”.
Otherwise, leaving “The application should check for updates” unchecked implies that updates will be handled manually. That’s what the code below does.
Plagarism note: This code is based on the MSDN article: How Do I: Use the ClickOnce Deployment API to Update My App?
Start a new Windows form project. Add a button to Form1. Then, put this code behind Form1:
Imports System.Deployment.Application
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ad As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
Dim info As UpdateCheckInfo = ad.CheckForDetailedUpdate
If Not info.UpdateAvailable Then
MsgBox("No updates are availible.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Notice")
Return
End If
Dim DoUpdate As Boolean = True
If info.IsUpdateRequired Then
MsgBox("There is a required update availible. Downloading now.", _
MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Notice")
Else
If MsgBox("Update availible; download now?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Question") = MsgBoxResult.No Then
DoUpdate = False
End If
End If
If DoUpdate Then
ad.Update()
MsgBox("The updates have been applied. The application must now be restarted.", _
MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Notice")
Application.Restart()
End If
End Sub
End Class
A good use of this might be to adapt the code to do the check asynchronously (in form_load, for instance), by changing the code that sets info’s value:
Dim info As UpdateCheckInfo = ad.CheckForUpdateAsync