下载文件和更新程序 [英] Download file and update program
问题描述
您好,
我尝试从googledrive下载文件,如果程序版本低于实际根据dropbox中的txt文件(1.0.0.1)。当我下载文件,标签显示文件大小和进度条下载过程时,这很好用,但文件没有下载到位。有谁知道为什么?我希望它能够工作:下载完成后程序退出并开始下载新的程序版本。
我尝试过:
Dim myCode As Byte()
Dim cesta_souboru As String =C:\ Filip \ Etikety\Debug
Dim file As String = cesta_souboru +\ file.txt
Dim wc As WebClient
Private Sub posli(ByVal sender As System.Object,ByVal e As System.EventArgs)处理check_update.Click
wc =新WebClient()
Dim filename =file.txt
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(https: //www.dropbox.com/s/uywfkhi7wdqa4en/Version.txt?dl=1)
昏暗响应As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO。 StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim newestversion As String = sr.ReadToEnd()
MsgBox(newestversion)
Dim currentversion As String = Application.ProductVersion
MsgBox(currentversion)
如果newestversion.Contains(currentversion)那么
MsgBox(Používáteaktuálníverzi)
Else
MsgBox(Nováverze programu jekestažení,aktualizuji jiprovás。)
尝试
System.IO.File.Delete(文件)
Catch ex As UnauthorizedAccessException
结束尝试
AddHandler wc.DownloadProgressChanged,AddressOf DownloadProgressChanged
'AddHandler wc.DownloadDataCompleted,AddressOf DownloaddataCompleted
wc.DownloadDataAsync(New Uri(https://drive.google.com/open?id=0B_pejckobLScTWJhUk9jasfhjk ),cesta_souboru +\+ filename)
End if
End Sub
Private Sub DownloadProgressChanged(ByVal sender As Object,ByVal e As DownloadProgressChangedEventArgs)
ProgressBar1 .Value = e.ProgressPercentage
lbl_prog_percent.Text = e.ProgressPercentage.ToString()+%
lbl_received.Text = String.Format({0} MB / s / {1} MB / s,(e.BytesReceived / 1024D / 1024D).ToString(),(e.TotalBytesToReceive / 1024D / 1024D).ToString( 0.00))
End Sub
Private Sub DownloaddataCompleted(ByVal sender As Object,ByVal e As DownloadDataCompletedEventArgs)
RemoveHandler wc.DownloadProgressChanged,AddressOf DownloadProgressChanged
RemoveHandler wc.DownloadDataCompleted,AddressOf DownloaddataCompleted
ProgressBar1.Value = 0
如果e.Error IsNot Nothing那么
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled然后
MessageBox。显示(由用户取消下载)
否则
'myCode = e.Result
结束如果
Dim URL As String = DirectCast(e.UserState,String)
MessageBox.Show(URL)
Timer5.Start()
End Sub
Private Sub Timer5_Tick(发送者为对象,e为EventArgs)处理Timer5。勾选
Process.Start(cesta_souboru +\ iMont ix.exe)
Application.Exit()
End Sub
< blockquote>我担心去年DropBox已经加强了安全性,使用下载URL并不简单。也许您可以尝试使用他们的API或使用其他提供程序。
除了Rick所说的,您的代码也会捕获异常并且不对它们执行任何操作:
尝试
System.IO.File.Delete(文件)
Catch ex As UnauthorizedAccessException
结束尝试
这隐藏了你捕获的异常,所以如果删除失败,你不知道它失败了,你也没有得到任何暗示。除非你有充分的理由这样做,否则吞下这样的例外是一个坏主意。
Hello,
I try to download file from googledrive if program version is lower then actual according txt file (1.0.0.1) in dropbox. When I download file, label show me size of file and progress bar download process, this works well, but file is not downloaded in location. Does anyone known why? I would like it to work: After download is completed program exit and start downloaded new program version.
What I have tried:
Dim myCode As Byte() Dim cesta_souboru As String = "C:\Filip\Etikety\Debug" Dim file As String = cesta_souboru + "\file.txt" Dim wc As WebClient Private Sub posli(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles check_update.Click wc = New WebClient() Dim filename = "file.txt" Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.dropbox.com/s/uywfkhi7wdqa4en/Version.txt?dl=1") 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() MsgBox(newestversion) Dim currentversion As String = Application.ProductVersion MsgBox(currentversion) If newestversion.Contains(currentversion) Then MsgBox("Používáte aktuální verzi") Else MsgBox("Nová verze programu je ke stažení, aktualizuji ji pro vás.") Try System.IO.File.Delete(file) Catch ex As UnauthorizedAccessException End Try AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged 'AddHandler wc.DownloadDataCompleted, AddressOf DownloaddataCompleted wc.DownloadDataAsync(New Uri("https://drive.google.com/open?id=0B_pejckobLScTWJhUk9jasfhjk"), cesta_souboru + "\" + filename) End If End Sub Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) ProgressBar1.Value = e.ProgressPercentage lbl_prog_percent.Text = e.ProgressPercentage.ToString() + "%" lbl_received.Text = String.Format("{0} MB/s / {1} MB/s", (e.BytesReceived / 1024D / 1024D).ToString(), (e.TotalBytesToReceive / 1024D / 1024D).ToString("0.00")) End Sub Private Sub DownloaddataCompleted(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs) RemoveHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged RemoveHandler wc.DownloadDataCompleted, AddressOf DownloaddataCompleted ProgressBar1.Value = 0 If e.Error IsNot Nothing Then MessageBox.Show(e.Error.Message) ElseIf e.Cancelled Then MessageBox.Show("Download cancelled by the user") Else 'myCode = e.Result End If Dim URL As String = DirectCast(e.UserState, String) MessageBox.Show(URL) Timer5.Start() End Sub Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer5.Tick Process.Start(cesta_souboru + "\iMontix.exe") Application.Exit() End Sub
I'm afraid DropBox has tightened their security last year, and it is not simple to use the download URL's anymore. Maybe you can try using their API or use another provider.
On top of what Rick said, your code is also catching exceptions and doing nothing with them:
Try System.IO.File.Delete(file) Catch ex As UnauthorizedAccessException End Try
This hides the exception your catching so if the Delete does fail, you have no idea it failed nor do you get any hint as to why. Unless you've got a REALLY good reason for doing so, swallowing exceptions like this is a bad idea.
这篇关于下载文件和更新程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!