Reputation: 61
i'm trying to find a way to let my users find out if an update exist. what i'm trying to do is to connect my user to my Dropbox an get the current update version from it.
here is my code (wich is not really mine...)
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
My problam is that no matter what i'm getting the msg "You are not up todate!".
Can anyone tell me why?
BTW the address to my dropbox changed by me before publishing my question to "MyDropbox"
Upvotes: 2
Views: 263
Reputation: 2245
There's the reasons why your code isn't appropriate :
So, instead of using this type of link : https://www.dropbox.com/s/MyDropBox/Version.txt?dl=0
, you should use this one : https://dl.dropboxusercontent.com/s/MyDropBox/Version.txt?dl=0
which display the raw content of the file.
There's the code I'm using in one of my softwares :
It requires a BackgroundWorker
(required to don't freeze the UI) and a 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
Your Update.txt
file can contain only the latest version.
Edit : The advantage with the BackgroundWorker is that it won't freeze the UI. So, in example, you can add a ProgressBar
.
Example of improvement :
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
To change the ProgressBar Value :
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
And you must set the BackgroundWorker WorkerReportsProgress
property to True
.
Upvotes: 2