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

时间:2018-09-27 17:46:59

标签: vb.net deployment clickonce auto-update intranet

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

有时它会按预期运行,有时应用程序会冻结,有时会更新4次。

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

该应用程序已通过网络部署,有时用户将使该应用程序运行数周而不注销。发布更新后,由于新功能或数据库更改,它们可能会出错。

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

  

这是表单加载事件...

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

  

这是Timer_Tick事件...

 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
  

这是CheckForUpdateDue函数...

 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

2 个答案:

答案 0 :(得分:0)

看来My.Application.Deployment.CheckForUpdate()是可行的方式。它检查UpdateLocation以查看是否有可用的更新,并要求用户下载该更新,或者您可以使用My.Application.Deployment.CheckForUpdate(true)强制执行无提示更新。

我尝试在调试模式下在项目上使用它,但它抛出了InvalidDeploymentException,但我认为它应该可以在Click-Once部署的生产代码中使用

答案 1 :(得分:0)

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

 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 似乎在工作 会拭目以待.....