1

I need to download file from FTP server and make some changes on it and upload it again to the same FTP using VB.NET.

Any help please. Thank you.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Amr Elnashar
  • 1,739
  • 12
  • 33
  • 53
  • I was looking for similar solution and found nice code by THE AMAZING. Check link below: http://stackoverflow.com/questions/5938893/using-ftp-to-download-each-file-while-getting-the-file-list – GiorgiTBS Dec 12 '16 at 20:00

2 Answers2

1

Some links:

VB.NET: http://www.codeproject.com/KB/IP/FtpClient.aspx

c#: http://www.c-sharpcorner.com/uploadfile/neo_matrix/simpleftp01172007082222am/simpleftp.aspx

Jason
  • 86,222
  • 15
  • 131
  • 146
  • this is not free dll. file. I need sample access direct Microsoft library – Amr Elnashar Mar 15 '10 at 20:06
  • The code is public domain and you should be able to access the classes from your VB.NET application. What else do you need? – Isak Savo Mar 15 '10 at 20:17
  • I tried the FTpclient.cs class from codeproject.com but when i try to download file from my ftp it gives me error file not available or no access, and I think its no access error, how can I access downloading files from ftp. thank you – Amr Elnashar Mar 16 '10 at 16:09
0

If you want to just directly re-upload the file, simply pipe the download stream to the upload stream:

Dim downloadRequest As FtpWebRequest =
    WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt")
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile
downloadRequest.Credentials = New NetworkCredential("username1", "password1")

Dim uploadRequest As FtpWebRequest =
    WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt")
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
uploadRequest.Credentials = New NetworkCredential("username2", "password2")

Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(),
      sourceStream As Stream = downloadResponse.GetResponseStream(),
      targetStream As Stream = uploadRequest.GetRequestStream()
    sourceStream.CopyTo(targetStream)
End Using

If you need to process the contents somehow, or if your need to monitor the progress, or both, you have to do it chunk by chunk (or maybe line by line, if it is a text file, that you are processing):

Dim downloadRequest As FtpWebRequest =
    WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt")
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile
downloadRequest.Credentials = New NetworkCredential("username1", "password1")

Dim uploadRequest As FtpWebRequest =
    WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt")
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
uploadRequest.Credentials = New NetworkCredential("username2", "password2")

Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(),
      sourceStream As Stream = downloadResponse.GetResponseStream(),
      targetStream As Stream = uploadRequest.GetRequestStream()
    Dim buffer As Byte() = New Byte(10240 - 1) {}
    Dim read As Integer
    Do
        read = sourceStream.Read(buffer, 0, buffer.Length)
        If read > 0 Then
            ' process "buffer" here
            targetStream.Write(buffer, 0, read)
        End If
    Loop While read > 0
End Using

See also:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992