Pages

Monday, January 31, 2011

Copy FTP File

 Private Function GetFtpServerCopy(ByVal ftpServer As String, ByVal username As String, _
                                           ByVal password As String, ByVal filepath As String) As Date
        Dim ftpFile As String
        If Not ftpServer.EndsWith("/") Then
            ftpServer &= "/"
        End If
        ftpFile = filepath + "/Logs/scanReport.csv"
        Dim dateStr As String = String.Empty

        Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpFile)
        Dim creds As New System.Net.NetworkCredential(username, password)
        request.Credentials = creds
        request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

        Dim response As System.Net.FtpWebResponse = request.GetResponse
     

        Using responseStream As IO.Stream = response.GetResponseStream
            'loop to read & write to file
            Using fs As New IO.FileStream("D:\copy.csv", IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Do
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                Loop Until read = 0 'see Note(1)
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using

        response.Close()
    End Function

No comments:

Post a Comment