【问题标题】:Loading file contents from FTP to ListBox将文件内容从 FTP 加载到 ListBox
【发布时间】:2018-07-27 22:37:02
【问题描述】:

当我尝试将文件内容从 FTP 加载到 ListBox 时遇到错误。

Form1(照片1)

Ftp Home Including All Ftp Server File's(图二)

Error issue(照片3),在“Ann File”(按钮)中按下时发生错误

Form1 代码(表单类)

Imports System.IO
Imports System.Net

Public Class Form1

    Dim client As New Net.WebClient
    Dim streamreaddder As IO.StreamReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()
        Dim ftp As FtpWebRequest =
            DirectCast(WebRequest.Create("ftp://Example.com/Ann.txt/"), FtpWebRequest)
        ftp.Method = WebRequestMethods.Ftp.ListDirectory
        Dim ftpFiles As New ArrayList()
        ftp.Credentials = New NetworkCredential("*****", "****")
        Dim Response As FtpWebResponse = ftp.GetResponse()
        Dim responseStream As Stream = Response.GetResponseStream()
        Dim reader = New StreamReader(responseStream)

        While Not (reader.EndOfStream)
           ftpFiles.Add(reader.ReadLine())
        End While
        For Each file In ftpFiles
          ListBox1.Items.Add(file)
        Next
        reader.Close()
        responseStream.Close()
        Response.Close()
    End Sub

End Class

只有我想在我的列表框中加载我的 FTP 文件 (Ann.txt)。

错误详情:

System.Net.WebException
HResult=0x80131509
Message=The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Source=System
StackTrace:
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at WindowsApp1.Form1.Button2_Click(Object sender, EventArgs e) in C:\Users\Ahmad\AppData\Local\Temporary Projects\WindowsApp1\Form1.vb:line 72
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApp1.My.MyApplication.Main(String[] Args) in :line 81

【问题讨论】:

    标签: .net vb.net visual-studio ftp ftp-client


    【解决方案1】:

    要读取文件内容,你必须使用DownloadFile方法,而不是ListDirectory

    Dim request As FtpWebRequest = WebRequest.Create("ftp://example.com/path/Ann.txt")
    request.Method = WebRequestMethods.Ftp.DownloadFile
    request.Credentials = New NetworkCredential("username", "password")
    
    Using response As FtpWebResponse = request.GetResponse(),
          stream As Stream = response.GetResponseStream(),
          reader As StreamReader = New StreamReader(stream)
        While Not reader.EndOfStream
            ListBox1.Items.Add(reader.ReadLine())
        End While
    End Using
    

    另请注意:

    • 使用Using声明;
    • 不需要中间的ArrayList()

    由于.GetResponseStream 返回的Stream 的糟糕实现,如果最后一行没有被新行终止,则代码将无法正常工作。解决这个问题需要更复杂的代码。 另见StreamReader ReadLine throwing disposed exception rather than returning null(不完全相同的问题,但类似)。

    【讨论】:

    • 将 Ftp 文件服务器导入我的 Listbox 很重要,为了新的未来,喜欢编辑它,或者在 Ann.txt 中添加新项目......等等
    • 我还检查了 Ann.Txt,它存在于我的 ftp 服务器中,所以我制作了按钮 1(用于服务器主目录)和按钮 2(用于现有文件 Ann.txt)(正如我解释所有在我的代码中)
    • 按钮 1(FTP 主页):将引导我访问 Ftp 服务器主页 ///// 按钮 2(Ann 文件):将引导我访问内部的 Ann.Txt 所以将列出(Ann.txt)行 ---- 例如我按下按钮 2 将在列表框中显示 (Ann.txt) 列表框 1 行中的条目
    • 是的,例如 (Ann.txt) 内容这行: - worker 1 - Worker 2 - worker 3 -=-= 然后将在列表框中显示 listbox1(Ann.txt)行3 行(工人 1,2,3)
    • 错误查看文本:justpaste.it/1hdej 错误查看图片:imgur.com/NRKFQO1
    猜你喜欢
    • 2018-08-02
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多