1

To read a file contents to ListBox, I used DownloadFile method from my previous question
Loading file contents from FTP to ListBox

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

Now I want to add another button that uses StreamWriter and WebRequest.GetRequestStream to upload the contents of ListBox back to FTP.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Thx,, The code was in https://stackoverflow.com/questions/48836591/the-remote-server-returned-an-error-550-listbox -=-= and someone tell me to make a new Question here again – ibrahim amen Feb 22 '18 at 09:30
  • A _"How do I do this"_ question is rarely acceptible at Stack Overflow. You need to do your own research and attempts. -- Also, **really** need to accept [**the answer Martin gave you**](https://stackoverflow.com/a/48839333), seeing as you're using his code. To accept an answer press the **tick/check mark** located on the left of Martin's post so that it turns green, like in this image: http://i.stack.imgur.com/LkiIZ.png -- This is a **very** important thing to do as it shows your gratitude towards the answerer _and_ rewards him/her for helping you! – Visual Vincent Feb 22 '18 at 09:30
  • thanks,, that what i was want to make ,, i was new in stackoverflow, then he ask me for Accept the answer ,, and i didnt got it clearly ,, thanks for your important info that you gived me – ibrahim amen Feb 22 '18 at 09:35
  • Let me ask you this: _What_ is it specifically that you want to upload when clicking `Button3`? – Visual Vincent Feb 22 '18 at 09:44
  • okey about Button 3 if i edit listbox (add lines + Edit some lines) when Press will Upload that lines As.txt into server again) – ibrahim amen Feb 22 '18 at 09:48

1 Answers1

2

You basically need the code from my answer to
How to download file from FTP and upload it again.


Though to easily copy contents (lines) of ListBox, you can slightly modify it to write line-by-line using StreamWriter:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim request As FtpWebRequest =
        WebRequest.Create("ftp://example.com/path/Ann.txt")
    request.Method = WebRequestMethods.Ftp.UploadFile
    request.Credentials = New NetworkCredential("username", "password")
    request.UseBinary = False

    Using stream As Stream = request.GetRequestStream(),
          writer As StreamWriter = New StreamWriter(stream)
        For index As Integer = 0 To ListBox1.Items.Count - 1
            writer.WriteLine(ListBox1.Items(index))
        Next
    End Using
End Sub
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992