自动更新 Click-Once 网络部署应用程序的可靠方法

Reliable way to automatically update Click-Once network deployed application

我已经为此工作了几个月,但没有取得真正的成功。 我从各种来源拼凑了代码,这就是我所拥有的。

有时它按预期工作,有时应用程序只是冻结,有时更新 4 次。

我有一个 5 分钟的计时器间隔,用于检查是否有更新可用以及是否有更新和重新启动。简单吧?

该应用程序是网络部署的,有时用户会离开该应用程序 运行 数周而不注销。发布更新时,他们可能会因为新功能或数据库更改而出错。

为什么会有如此不同的结果?

Here is the Form Load event...

Me.Timer2.Interval = 300000 Me.Timer2.Start()

Here is the Timer_Tick Event...

 Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

    If CheckForUpdateDue() Then
        My.Forms.PopupLoading.Show()
        My.Forms.PopupLoading.Label1.Text = "Project is Updating Please Wait..."
        My.Forms.PopupLoading.Refresh()
        Dim ad As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
        ad.Update()
        My.Settings.Save()
        My.Forms.PopupLoading.Close()
        My.Forms.Popup_Restarting.Show()
        Application.Restart()
    End If

End Sub

Here is the CheckForUpdateDue Function...

 Private Function CheckForUpdateDue() As Boolean
    Dim isUpdateDue As Boolean = False
    If ApplicationDeployment.IsNetworkDeployed Then
        Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
        If AD.CheckForUpdate() Then
            isUpdateDue = True
        End If
    End If
    CheckForUpdateDue = isUpdateDue
End Function

Application Settings

Application Update Settings

看起来 My.Application.Deployment.CheckForUpdate() 是正确的选择。它检查 UpdateLocation 以查看是否有更新可用并要求用户下载它,或者您可以使用 My.Application.Deployment.CheckForUpdate(true) 强制静默更新。

我尝试在我的项目中以调试模式使用它,它抛出一个 InvalidDeploymentException,但我认为它应该在生产代码中使用 Click-Once Deployment

这就是我最终得到的...

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim ct As Date = DateTime.Now
    If ct.Hour = 22 Then
        Dim info As UpdateCheckInfo = Nothing

        If ApplicationDeployment.IsNetworkDeployed Then
            Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment

            Try
                info = AD.CheckForDetailedUpdate()
            Catch dde As DeploymentDownloadException
                MessageBox.Show("The new version of the application cannot be downloaded at this time. " & ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later. Error: " + dde.Message)
                Return
            Catch ioe As InvalidOperationException
                MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " & ioe.Message)
                Return
            End Try

            If info.UpdateAvailable Then
                Dim doUpdate As Boolean = True

                If doUpdate Then
                    Try
                        'restart application first
                        Application.Restart()
                        'then do update
                        AD.Update()
                        'when the application is restarted again it will be update
                    Catch dde As DeploymentDownloadException
                        MessageBox.Show("Cannot install the latest version of the application. " & ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later.")
                        Return
                    End Try
                End If
            End If
        End If
    End If

End Sub

取自Microsoft VS Docs 似乎在工作 拭目以待.....