1

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
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75

1 Answers1

2

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • it important to Direct Ftp File Server into my Listbox, for new future , Like Edit it, Or Add New items inThe Ann.txt ... Etc – ibrahim amen Feb 17 '18 at 12:31
  • Also i checked Ann.Txt ,, It Exist In my ftp server , So i made Button 1 ( For server home Directory) And Button 2 (For Exist File Ann.txt) (As i explain all that in my Code) – ibrahim amen Feb 17 '18 at 12:33
  • Button 1 (FTP Home) : Will Direct me for Ftp Server home ///// Button 2 (Ann file) : Will Direct me For Ann.Txt inside So will list (Ann.txt) Lines ---- For Example i presed Button 2 Will Show Me in listbox (Ann.txt) Entries in listbox1 lines – ibrahim amen Feb 18 '18 at 17:14
  • yees,, for example (Ann.txt) Contents this lines : - worker 1 - Worker 2 - worker 3 -=-= then will show listbox1 (the Ann.txt) lines in listbox for this 3 lines (Worker 1,2,3) – ibrahim amen Feb 20 '18 at 03:05
  • Error View Text : https://justpaste.it/1hdej Error View Picture : https://imgur.com/NRKFQO1 – ibrahim amen Feb 21 '18 at 01:32
  • Big thx ,, its now fine .,, So last question before close that case,, i greatful for your that help,, can i make " UploadFile " , after my listbox edited ,can i re uploaded it in same Ann.txt with Button 3 (button 2 to downloadFile) Button 3 for re upload it again from listbox) ,, so Uploadfile method, is it same like downloafile method ?! – ibrahim amen Feb 21 '18 at 10:52
  • Sorry bro., Can you give me that button 3 , Code , by this i will know more about that, ,, – ibrahim amen Feb 21 '18 at 23:28
  • i appreciate it, i only said Bro = Brother and it was little thing that i do for you great help/,, Any way thanks,, i also upload new question as you say,. – ibrahim amen Feb 22 '18 at 08:41
  • I know the Stackoverflow the best forever for his Dev's , Programmer's helpful, – ibrahim amen Feb 22 '18 at 08:51
  • excuse me ,, i'm new in stackoverflow, i now got what you mean about Accepting the answer, my sorry for that ,, Also im new on it ,, and dont have access to Vote up, at least i have to collect 15 reputation, i try to figure About to how to accept your question ,, Thx – ibrahim amen Feb 22 '18 at 09:28
  • i got it,, Almost checked that accepted,, thanks Martin ,, i didnt was joken ,, i only didt got it clearly for your Accept that question , i didt catch how to perform that Accepted By thank,, Hope that make you fell better ,, Thx – ibrahim amen Feb 22 '18 at 09:38