如何逐行从URL中读取文本文件

时间:2015-06-05 09:34:58

标签: vb.net

我正在使用vb.net(我是业余爱好者),我正在尝试让我的程序从我的ftp服务器下载文件。 它应该从链接中逐行读取文本文件,并且每行都是一个单词。

文本文件类似于:

  • 第一
  • 第二
  • 第三

必须将每行内容添加到链接然后下载。下载第一行后,它必须转到第二行,依此类推。 我不知道它是否可能,我真的希望有人可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

此代码可满足您的需求:

Dim FileLink As String = "yourserver/file.txt" 'Link to your text file
Dim Destination As String = "C:\" 'Link to the download directory
Dim Client As WebClient = New WebClient() 'New web client
Dim Reader As StreamReader = New StreamReader(Client.OpenRead(FileLink)) 'A stream reader to get your file contents
Dim A As String = Reader.ReadToEnd 'Outputing the file contents to A
Dim Phase As Integer = 0 'A phase which determens which line you're at
Dim Links(500) As String 'A string array to hold 500 links, you can edit the number
For I As Integer = 0 To A.Length 'Looping through A
If A(I) = Environment.NewLine Then 'If the current character is a newline, it'll start downloading the from the first link and increase the Phase 
My.Computer.Network.DownloadFile(Links(Phase), Destination, "", "", False, 9800, True) 'Link, destination, id, password, show UI or not, timeout, overwrite or not
Phase += 1
Else 'If it's not a newline then the link is still incomplete, it adds the character to the link currently being written
Links(Phase) += A(I)
End If
Next