将文件内容从FTP加载到ListBox [英] Loading file contents from FTP to ListBox
问题描述
当我尝试从FTP加载文件内容到 ListBox
时,我遇到了一个错误。
Form1 (Photo1)
Ftp主页包括所有Ftp服务器文件(照片2)
错误问题(照片3),按下时发生错误inAnn File(Button)
Form1 Code(Form Class)
Imports System.IO
Imports System.Net
公共类Form1
Dim client As New Net.WebClient
Dim streamreaddder As IO.StreamReader
Private Sub Form1_Load(sender As Object,e As EventArgs)处理MyBase.Load
End Sub
Private Sub Button2_Click(sender As Object,e As EventArgs)处理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响应为FtpWebResponse = ftp.GetResponse()
Dim responseStream As Stream = Response.GetResponseStream()
Dim reader = New StreamReader(responseStream)
虽然Not(reader.EndOfStream)
ftpFiles.Add(reader.ReadLine())
结束时
对于每个文件在ftpFiles
ListBox1.Items.Add(文件)
下一个
reader.Close()
responseStream.Close()
Response.Close()
End Sub
End Class
只有我想在我的列表框中加载我的FTP文件( Ann.txt
)。
错误详情:
System.Net.WebException
HResult = 0x80131509
消息=远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问)。
Source = System
StackTrace:System.Net.FtpWebRequest.SyncRequestCallback上的
(Object obj)$ System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(布尔处置)System.IO.Stream.Close()上的
System.Net.ConnectionPool上的System.IO.Stream.Dispose()上的
。在System.Net.ConnectionPool.PutConnection(PooledStream pooledStream,Object owningObject,Int32 creationTimeout,Boolean canReuse)
(在System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段)
在C:\ Users \Ahmad\AppData\Local\Temporary Projects\WindowsApp1\\中WindowsApp1.Form1.Button2_Click(Object sender,EventArgs e)处的System.Net.FtpWebRequest.GetResponse()
\\Form1.vb:第72行$ System.Windows.Forms.Control.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnClick(EventArgs e)
在系统。 Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在System.Win dows.Forms.Control.WmMouseUp(消息&安培; 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)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& amp; m) ; 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
ListDirectory code $:
$ p $ Dim request As FtpWebRequest = WebRequest.Create(ftp://example.com/path/ Ann.txt)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential(username,password)
使用响应为FtpWebResponse = request.GetResponse(),
Stream作为Stream = response.GetResponseStream(),
reader作为StreamReader =新的StreamReader(流)
虽然不是reader.EndOfStre am
ListBox1.Items.Add(reader.ReadLine())
End While
End使用
另请注意:
- 使用
使用
语句;
- 不需要中间
ArrayList()
。
由于返回的 Stream
的糟糕实现,通过 .GetResponseStream
,如果最后一行没有被新行终止,代码将无法正常工作。解决这个问题需要更复杂的代码。
另请参见 StreamReader ReadLine抛出处置异常而不是返回空(不完全相同的问题,但类似)。
I face an error when I try to load a contents of a file from FTP to ListBox
.
Form1 (Photo1)
Ftp Home Including All Ftp Server File's (Photo2)
Error issue (Photo3), Error Happen When Press in "Ann File" (Button)
Form1 Code (Form Class)
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
Only I want to load my FTP file (Ann.txt
) in my listbox .
Error details:
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
解决方案 To read a file contents, you have to use DownloadFile
method, not 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
Also note:
- the use of
Using
statement;
- no need for intermediate
ArrayList()
.
Due to crappy implementation of the Stream
returned by .GetResponseStream
, the code will not work correctly if the last line is not terminated by a new line. Working this around would require much more complicate code.
See also StreamReader ReadLine throwing disposed exception rather than returning null (not exactly the same problem, but similar).
这篇关于将文件内容从FTP加载到ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!