Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将文件内容从FTP加载到ListBox_.net_Vb.net_Visual Studio_Ftp_Ftp Client - Fatal编程技术网

.net 将文件内容从FTP加载到ListBox

.net 将文件内容从FTP加载到ListBox,.net,vb.net,visual-studio,ftp,ftp-client,.net,Vb.net,Visual Studio,Ftp,Ftp Client,当我试图将文件内容从FTP加载到列表框时,会遇到一个错误 (照片一) (照片二) (照片3),按下“Ann文件”(按钮)时出错 表格1代码(表格类别) 只有我想在我的列表框中加载我的FTP文件(Ann.txt) 错误详细信息: System.Net.WebException HResult=0x80131509 Message=远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。 来源=系统 堆栈跟踪: 在System.Net.FtpWebRequest.SyncReques

当我试图将文件内容从FTP加载到
列表框
时,会遇到一个错误

(照片一)

(照片二)

(照片3),按下“Ann文件”(按钮)时出错

表格1代码(表格类别)

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

错误详细信息:

System.Net.WebException
HResult=0x80131509
Message=远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。
来源=系统
堆栈跟踪:
在System.Net.FtpWebRequest.SyncRequestCallback中(对象obj)
在System.Net.FtpWebRequest.RequestCallback(对象obj)中
位于System.Net.CommandStream.Dispose(布尔处理)
在System.IO.Stream.Close()处
在System.IO.Stream.Dispose()处
位于System.Net.ConnectionPool.Destroy(PooledStream PooledStream)
在System.Net.ConnectionPool.PutConnection(PooledStream PooledStream、对象所有者Object、Int32 creationTimeout、布尔值canReuse)
在System.Net.FtpWebRequest.FinishRequestStage(请求阶段)中
在System.Net.FtpWebRequest.GetResponse()中
在WindowsApp1.Form1.Button2\u单击C:\Users\Ahmad\AppData\Local\Temporary Projects\WindowsApp1\Form1.vb中的(对象发送者,事件参数e):第72行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.ButtonBase.WndProc(Message&m)
在System.Windows.Forms.Button.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()上
在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()上
在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]命令行)
在WindowsApp1.My.MyApplication.Main(字符串[]Args)中:第81行

要读取文件内容,必须使用
下载文件
方法,而不是
列表目录

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

另请注意:

  • 使用
    使用
    语句
    
  • 不需要中间
    ArrayList()

由于
.GetResponseStream
返回的
流的实现糟糕,如果最后一行没有被新行终止,代码将无法正常工作。解决这个问题需要更复杂的代码。

另请参见(不是完全相同的问题,但类似)。

重要的是将Ftp文件服务器导入我的列表框,以获得新的未来,如编辑它,或在Ann.txt中添加新项目。。。EtcAlso我检查了Ann.Txt,它存在于我的ftp服务器中,所以我制作了按钮1(用于服务器主目录)和按钮2(用于存在文件Ann.Txt)(正如我在代码中解释的那样)按钮1(ftp主目录):将指示我进入ftp服务器主目录//将指示我进入Ann.Txt,因此将列出(Ann.Txt)行----例如,我显示按钮2将在listbox1 LinesYes中的listbox(Ann.txt)条目中显示我,例如(Ann.txt)内容此行:-worker 1-worker 2-worker 3-=-=然后将在listbox1(Ann.txt)行中显示此3行(worker 1,2,3)的错误视图文本:错误视图图片:
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