Download files from an FTP site


This code help to download files from an FTP site

This code help to download files from an FTP site




Public Function DownloadFileByFTP(ByVal destFileName As String, ByVal downloadUri As Uri) As FtpStatusCode
Try
If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTp site")
End If

Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(downloadUri), FtpWebRequest)

If Me.m_isAnonymousUser = False Then
ftpRequest.Credentials = New NetworkCredential(Me.m_userName, Me.m_password)
End If

ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile

Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
Dim stream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim writer As StreamWriter = Nothing

Try
stream = ftpResponse.GetResponseStream
reader = New StreamReader(stream, Encoding.UTF8)
writer = New StreamWriter(destFileName, False)
writer.Write(reader.ReadToEnd)
Return ftpResponse.StatusCode
End Try
Catch ex As Exception
Throw ex
End Try
End Function


Related Articles

More articles: Download FTP files FTP file hierarchy Get FTP directory

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: