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

发布于 2025-01-17 23:15:46 字数 552 浏览 2 评论 0原文

这是我到目前为止的代码,

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())

我对如何将 reader.ReadToEnd 的内容放入 ListBox 中感到非常困惑。如果有人可以帮助我解决这个问题,我将不胜感激。

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. If anyone can assist me with this issue it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-24 23:15:46

使用 逐行读取流StreamReader.ReadLine

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

如果处理大量文件时的性能不是问题,以下低效但简短的代码也可以实现相同的效果:

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

请注意,您应该在后台线程上执行网络操作,但是这是一个单独的 问题。

Read the stream line-by-line using StreamReader.ReadLine

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文