我正在尝试创建一个简单的下载应用程序,该应用程序从文本文件和下载文件的所需文件名中获取链接,然后下载它。 我已经完成了代码,但是当我运行应用程序时,没有任何反应。没有运行时错误,我无法在代码或逻辑中找出问题。
以下是完整的代码:
Imports System.Net
Public Class Form1
' the file has filename,link. Each even number is a filename, and odd number is a link
' program loads it seperately into different array
' then download the file with and save with it's filename
Dim WithEvents WC As New WebClient
Private Sub backgroundDownloader_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles backgroundDownloader.DoWork
Dim lines() As String = IO.File.ReadAllLines("down.txt")
Dim counter As Integer = 0
Dim file As String
Dim path = "downloads\"
' filenames will be the same amount as links and vise-versa
' that means filenames and links are exactly half the size of total lines of files
Dim filenames(lines.Length / 2) As String
Dim links(lines.Length / 2) As String
' store value in arrays
While counter < lines.Length
' using modulus operator to test odd or even counter
If counter Mod 2 = 0 Then
filenames(counter) = lines(counter)
counter += 1
Else
links(counter) = lines(counter)
counter += 1
End If
End While
counter = 0
' add path to each filename
Dim temp As String
For Each file In filenames
temp = ""
temp = path + file
file = temp
Next
'start download
While counter < links.Length
WC.DownloadFileAsync(New Uri(links(counter)), filenames(counter))
counter += 1
End While
End Sub
Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
' update progressbar status
progressBar1.Value = e.ProgressPercentage
' change status when done status when download completes
If progressBar1.Value = 100 Then
lblStatus.Text = "Downloads Completed!"
picStatus.Image = Image.FromFile("resources\images\done.png")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
backgroundDownloader.RunWorkerAsync()
End Sub
End Class
以下是down.txt(存储名称和链接的文件)的内容:
first.bin
https://speed.hetzner.de/100MB.bin
second.bin
https://speed.hetzner.de/100MB.bin
third.bin
https://speed.hetzner.de/100MB.bin
fourth.bin
https://speed.hetzner.de/100MB.bin
callstack没有显示任何内容。它在内存和变量窗口中都是空白的,如屏幕截图所示:screenshot
任何帮助或建议都会表示赞赏。谢谢。
编辑(1) - 增加计数器变量。
EDIT(2) - 现在使用临时字符串添加文件名
的路径