应用程序更新检查问题

时间:2015-08-20 10:41:05

标签: vb.net

我试图找到一种方法让我的用户知道是否存在更新。 我想要做的是将我的用户连接到我的Dropbox并获得最新信息 从它更新版本。

这是我的代码(这不是我的......)

 Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(" https://www.dropbox.com/s/MyDropBox/Version.txt?dl=0")
    Dim response As System.Net.HttpWebResponse = request.GetResponse()
    Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
    Dim newestversion As String = sr.ReadToEnd()
    Dim currentversion As String = Application.ProductVersion

    If newestversion.Contains(currentversion) Then
        MsgBox("You are up todate!")
    Else
        MsgBox("You are not up todate!")
    End If

我的问题在于,无论我收到什么信息,你都不会感到高兴!"。

谁能告诉我为什么?

在将问题发布到" MyDropbox"

之前,我改变了我的Dropbox地址

1 个答案:

答案 0 :(得分:2)

您的代码不合适的原因

  • 此过程太长
  • 它返回太多文字
  • 您不使用正确的Dropbox类型的链接
  • 并且显然,返回的文字不包含您想要的文字(文件内容)。

所以,不要使用这种类型的链接:https://www.dropbox.com/s/MyDropBox/Version.txt?dl=0你应该使用这个https://dl.dropboxusercontent.com/s/MyDropBox/Version.txt?dl=0 显示原始内容文件。

我正在使用我的一个软件中的代码:

它需要BackgroundWorker(不要冻结用户界面)和Button

Imports System.Net

Public Class Form1

    Private UpdateLink As String = "https://dl.dropboxusercontent.com/s/5ogqwr9kc31r61w/Update.txt?dl=0"
    Private InstalledVersion As String = Application.ProductVersion
    Private UpToDate As String = "UpToDate"
    Private Outdated As String = "Outdated"
    Private LatestVersion As String
    Private MAJ As New WebClient

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
        Button1.Enabled = False
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            'Original link is https://www.dropbox.com/s/5ogqwr9kc31r61w/Update.txt?dl=0
            'But it redirects to the file with Dropbox online interface.
            'If you replace "www.dropbox" by "dl.dropboxusercontent", the new link will redirect to the raw text.
            'New link is https://dl.dropboxusercontent.com/s/5ogqwr9kc31r61w/Update.txt?dl=0

            LatestVersion = MAJ.DownloadString(UpdateLink)

            If LatestVersion.Contains(InstalledVersion) Then
                e.Result = UpToDate
            Else
                e.Result = Outdated
            End If

            MAJ.Dispose()
        Catch ex As Exception
            e.Result = ex.Message
        End Try
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Button1.Enabled = True

        Select Case e.Result
            Case UpToDate
                MsgBox("It's up to date (Latest : " & LatestVersion & ", Installed : " & InstalledVersion & ")")
            Case Outdated
                MsgBox("It's outdated (Latest : " & LatestVersion & ", Installed : " & InstalledVersion & ")")
            Case Else
                MsgBox(e.Result)
        End Select
    End Sub

End Class

您的Update.txt文件只能包含最新版本。

编辑:BackgroundWorker的优势在于它不会冻结用户界面。因此,在示例中,您可以添加ProgressBar

改进的例子:

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            BackgroundWorker1.ReportProgress(20)
            BackgroundWorker1.ReportProgress(30)

            'Original link is https://www.dropbox.com/s/5ogqwr9kc31r61w/Update.txt?dl=0
            'But it redirects to the file with Dropbox online interface.
            'If you replace "www.dropbox" by "dl.dropboxusercontent", the new link will redirect to the raw text.
            'New link is https://dl.dropboxusercontent.com/s/5ogqwr9kc31r61w/Update.txt?dl=0

            LatestVersion = MAJ.DownloadString(UpdateLink)
            BackgroundWorker1.ReportProgress(60)
            BackgroundWorker1.ReportProgress(80)

            If LatestVersion.Contains(InstalledVersion) Then
                BackgroundWorker1.ReportProgress(100)
                Threading.Thread.Sleep(1000)
                e.Result = UpToDate
            Else
                BackgroundWorker1.ReportProgress(100)
                Threading.Thread.Sleep(1000)
                e.Result = Outdated
            End If

            MAJ.Dispose()
        Catch ex As Exception
            BackgroundWorker1.ReportProgress(100)
            Threading.Thread.Sleep(1000)
            e.Result = ex.Message
        End Try
    End Sub

更改ProgressBar值:

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

您必须将BackgroundWorker WorkerReportsProgress属性设置为True