Using ApplicationDeployment to manually update ClickOnce applications

Class to implement manual updates in ClickOnce applications.

With one line of code, you can asynchronously update your applications

Dim updateCO As New UpdateClickOnce("EnGraph Manager", True, True)

Public Class UpdateClickOnce #Region " Variables " Dim WithEvents MyApp As Deployment.Application.ApplicationDeployment Dim AppName As String = "this application" Dim UseMsgbox As Boolean = True Dim IsChecking As Boolean = False #End Region

#Region " Properties " Public Property ApplicationName() As String Get Return AppName End Get Set(ByVal value As String) AppName = value End Set End Property Public Property UseMessageBoxes() As Boolean Get Return UseMsgbox End Get Set(ByVal value As Boolean) UseMsgbox = value End Set End Property #End Region

#Region " Events " Public Event CheckedForUpdate(ByVal UpdateAvailable As Boolean) Public Event ApplicationUpdated(ByVal e As EventArgs_Done) #End Region

#Region " Constructors " Public Sub New(ByVal ApplicationName As String, ByVal CheckNow As Boolean, ByVal UseMessageBoxes As Boolean) AppName = ApplicationName UseMsgbox = UseMessageBoxes If CheckNow = True Then Call CheckForUpdate() End If End Sub Public Sub New()

End Sub

#End Region

#Region " Methods " Public Sub CheckForUpdate() Try If IsChecking = True Then Exit Sub MyApp = My.Application.Deployment If My.Computer.Network.IsAvailable = True And My.Application.IsNetworkDeployed = True Then

            IsChecking = True
            MyApp.CheckForUpdateAsync()
        Else
            RaiseEvent ApplicationUpdated(New EventArgs\_Done(EventArgs\_Done.Reason.NoInternet))
        End If

    Catch ex As Exception
        RaiseEvent ApplicationUpdated(New EventArgs\_Done(EventArgs\_Done.Reason.Error\_Occured))
    End Try

End Sub

#End Region

#Region " ApplicationDeployment Events " Private Sub MyApp_CheckForUpdateCompleted(ByVal sender As Object, ByVal e As System.Deployment.Application.CheckForUpdateCompletedEventArgs) Handles MyApp.CheckForUpdateCompleted RaiseEvent CheckedForUpdate(e.UpdateAvailable) If e.UpdateAvailable = True Then MyApp.UpdateAsync() Else IsChecking = False RaiseEvent ApplicationUpdated(New EventArgs_Done(EventArgs_Done.Reason.NoUpdate)) End If End Sub

Private Sub MyApp\_UpdateCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles MyApp.UpdateCompleted
    IsChecking = False
    If (e.Cancelled) Then
        RaiseEvent ApplicationUpdated(New EventArgs\_Done(EventArgs\_Done.Reason.Cancelled))
        If UseMsgbox = True Then
            MsgBox("The update of " & AppName & " was cancelled")
        End If
        Exit Sub
    Else
        If (e.Error IsNot Nothing) Then
            RaiseEvent ApplicationUpdated(New EventArgs\_Done(EventArgs\_Done.Reason.Error\_Occured))
            If UseMsgbox = True Then
                MsgBox("The update of " & AppName & " encountered an error and was not updated ")
            End If
            Exit Sub
        End If
    End If

    RaiseEvent ApplicationUpdated(New EventArgs\_Done)
    If UseMsgbox = True Then
        If MsgBox(AppName & " has been successfully updated.  Restart now?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Application.Restart()
        End If
    End If
End Sub

#End Region

End Class Public Class EventArgs_Done Inherits EventArgs Dim _Success As Boolean = True Dim _failurereason As Reason = Reason.NoFailure Enum Reason NoInternet Cancelled Error_Occured NoUpdate NoFailure End Enum Public Property Success() As Boolean Get Return _Success End Get Set(ByVal value As Boolean) _Success = value End Set End Property Public Property FailureReason() As Reason Get Return _failurereason End Get Set(ByVal value As Reason) _failurereason = value End Set End Property Public Sub New()

End Sub
Public Sub New(ByVal FailureReason As Reason)
    \_failurereason = FailureReason
    \_Success = False
End Sub

End Class

This article is part of the GWB Archives. Original Author: Tim Hibbard

New on Geeks with Blogs