我用如下代码从指定网站下载某个文件(732KB)时,老出现下载到一部分就停止下载了,奇怪的是有时候能下,有时候不能下,请各位高手指教,谢谢!
代码如下:
Public Class Form1
Private WithEvents webClient As New Net.WebClient
Dim bz As Boolean = False
Private Sub webClient_DownloadProgressChanged(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles webClient.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
lbl_pro.Text = e.ProgressPercentage.ToString() + "%"
lbl_detail.Text = String.Format("正在下载文件,完成进度{0}/{1}(KB)", e.BytesReceived / 1024, e.TotalBytesToReceive / 1024)
End Sub
Private Sub webClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles webClient.DownloadFileCompleted
If e.Error Is Nothing Then
lbl_detail.Text = "下载正常完成"
End If
If e.Cancelled = True Then
lbl_detail.Text = "自动升级被取消了"
Else
lbl_detail.Text = "下载完毕,开始更新程序,请稍侯!"
If bz = False Then
If System.IO.File.Exists(Application.StartupPath & "\autoplayer.exe") = True Then
Shell(Application.StartupPath & "\autoplayer.exe")
End If
bz = True
End
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Network As New Microsoft.VisualBasic.Devices.Network
If webClient.IsBusy Then webClient.CancelAsync()
webClient.Credentials = New System.Net.NetworkCredential("taixinsoft", "Dircls9607") '
AddHandler webClient.DownloadProgressChanged, AddressOf webClient_DownloadProgressChanged
AddHandler webClient.DownloadFileCompleted, AddressOf webClient_DownloadFileCompleted
If Network.IsAvailable = True Then
webClient.DownloadFileAsync(New Uri("http://www.taixinsoft.com/taixinsoft/autoplayer/autoplayer.txt"), Application.StartupPath & "\autoplayer.exe")
End If
End Sub
Private WithEvents myWebclient As Net.WebClient
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
My.Computer.Network.DownloadFile("http://www.taixinsoft.com/taixinsoft/autoplayer/autoplayer.txt", Application.StartupPath & "\autoplayer.exe", "", "", True, 500, True)
End Sub
End Class
3 个解决方案
#1
头次用上CSDN发贴,不知道怎么贴图,再把图片贴到网站上,比较麻烦
#2
加断点,这个只能自己调试解决。
#3
Public Sub xiazai()
Dim remoteUri As String = "http://127.0.0.1:8080/BianWanBianXueWebservice\NewVersion_bxbw\"
Dim fileName As String = "ala.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
Dim f As String = 55
Console.WriteLine("正在下载文件 ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' 下载并保存到当前文件夹
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("成功下载了""{0}"" from ""{1}""", fileName, myStringWebResource)
Dim saveFath As String = Application.StartupPath & "\newVerSoft\"
Console.WriteLine((ControlChars.Cr + "下载的文件保存到:" + ControlChars.Cr + ControlChars.Tab + saveFath))
End Sub
Dim remoteUri As String = "http://127.0.0.1:8080/BianWanBianXueWebservice\NewVersion_bxbw\"
Dim fileName As String = "ala.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
Dim f As String = 55
Console.WriteLine("正在下载文件 ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' 下载并保存到当前文件夹
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("成功下载了""{0}"" from ""{1}""", fileName, myStringWebResource)
Dim saveFath As String = Application.StartupPath & "\newVerSoft\"
Console.WriteLine((ControlChars.Cr + "下载的文件保存到:" + ControlChars.Cr + ControlChars.Tab + saveFath))
End Sub
#1
头次用上CSDN发贴,不知道怎么贴图,再把图片贴到网站上,比较麻烦
#2
加断点,这个只能自己调试解决。
#3
Public Sub xiazai()
Dim remoteUri As String = "http://127.0.0.1:8080/BianWanBianXueWebservice\NewVersion_bxbw\"
Dim fileName As String = "ala.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
Dim f As String = 55
Console.WriteLine("正在下载文件 ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' 下载并保存到当前文件夹
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("成功下载了""{0}"" from ""{1}""", fileName, myStringWebResource)
Dim saveFath As String = Application.StartupPath & "\newVerSoft\"
Console.WriteLine((ControlChars.Cr + "下载的文件保存到:" + ControlChars.Cr + ControlChars.Tab + saveFath))
End Sub
Dim remoteUri As String = "http://127.0.0.1:8080/BianWanBianXueWebservice\NewVersion_bxbw\"
Dim fileName As String = "ala.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
Dim f As String = 55
Console.WriteLine("正在下载文件 ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' 下载并保存到当前文件夹
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("成功下载了""{0}"" from ""{1}""", fileName, myStringWebResource)
Dim saveFath As String = Application.StartupPath & "\newVerSoft\"
Console.WriteLine((ControlChars.Cr + "下载的文件保存到:" + ControlChars.Cr + ControlChars.Tab + saveFath))
End Sub