简体   繁体   English

将具有 FTP 目录列表的 StreamReader 内容添加到 VB.NET 中的列表框?

[英]Add StreamReader contents with FTP directory listing to a ListBox in VB.NET?

This is the code I have so far这是我到目前为止的代码

Dim listRequest As FtpWebRequest = WebRequest.Create("ftp://ftpserver.com/folder/")
listRequest.Credentials = New NetworkCredential(ftp_user, ftp_pass)
listRequest.Method = WebRequestMethods.Ftp.ListDirectory
Dim listResponse As FtpWebResponse = listRequest.GetResponse()
Dim reader As StreamReader = New StreamReader(listResponse.GetResponseStream())
ListBox1.Items.AddRange(reader.ReadToEnd())
MessageBox.Show(reader.ReadToEnd())

I am very confused on how I can put the contents of the reader.ReadToEnd in a ListBox .我对如何将reader.ReadToEnd的内容放入ListBox感到非常困惑。 If anyone can assist me with this issue it would be greatly appreciated.如果有人可以帮助我解决这个问题,将不胜感激。

Read the stream line-by-line using StreamReader.ReadLine使用StreamReader.ReadLine逐行读取 stream

ListBox1.BeginUpdate()
Try
    Dim line As String
    While True
        line = reader.ReadLine()
        If line IsNot Nothing Then
            ListBox1.Items.Add(line)
        Else
            Exit While
        End If
    End While
Finally
    ListBox1.EndUpdate()
End Try

If performance when dealing with huge amount of files is not a concern, the following inefficient but short code will do the same:如果处理大量文件时的性能不是问题,则以下低效但简短的代码将执行相同的操作:

ListBox1.Items.AddRange(
    reader.ReadToEnd().Split(
        New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries))

Note that you should be doing.network operations on a background thread, but that's for a separate question.请注意,您应该在后台线程上执行网络操作,但这是一个单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM