ftp file upload,download,delete,rename

VNote 2022.05.17 07:19:08 *:

Imports System.Net

Imports System.IO

 

Public Class Form1

 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

 

        Dim wrUpload As FtpWebRequest = DirectCast(WebRequest.Create _

           ("ftp://ftp.test.com/file.txt"), FtpWebRequest)

 

        'Specify Username & Password'

        wrUpload.Credentials = New NetworkCredential("user", _

           "password")

 

        'Start Upload Process'

        wrUpload.Method = WebRequestMethods.Ftp.UploadFile

 

        'Locate File And Store It In Byte Array'

        Dim btfile() As Byte = File.ReadAllBytes("c:\file.txt")

 

        'Get File'

        Dim strFile As Stream = wrUpload.GetRequestStream()

 

        'Upload Each Byte'

        strFile.Write(btfile, 0, btfile.Length)

 

        'Close'

        strFile.Close()

 

        'Free Memory'

        strFile.Dispose()

    End Sub

 

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        'Create Request To Download File'

        Dim wrDownload As FtpWebRequest = _

           WebRequest.Create("ftp://ftp.test.com/file.txt")

 

        'Specify That You Want To Download A File'

        wrDownload.Method = WebRequestMethods.Ftp.DownloadFile

 

        'Specify Username & Password'

        wrDownload.Credentials = New NetworkCredential("user", _

           "password")

 

        'Response Object'

        Dim rDownloadResponse As FtpWebResponse = _

           wrDownload.GetResponse()

 

        'Incoming File Stream'

        Dim strFileStream As Stream = _

           rDownloadResponse.GetResponseStream()

 

        'Read File Stream Data'

        Dim srFile As StreamReader = New StreamReader(strFileStream)

 

        Console.WriteLine(srFile.ReadToEnd())

 

        'Show Status Of Download'

        Console.WriteLine("Download Complete, status {0}", _

           rDownloadResponse.StatusDescription)

 

        srFile.Close() 'Close

 

        rDownloadResponse.Close()

    End Sub

 

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        'Create Request To Delete File'

        Dim wrDelete As FtpWebRequest = _

           CType(WebRequest.Create("ftp://ftp.test.com/file.txt"),  _

           FtpWebRequest)

 

        'Specify That You Want To Delete A File'

        wrDelete.Method = WebRequestMethods.Ftp.DeleteFile

 

        'Response Object'

        Dim rDeleteResponse As FtpWebResponse = _

           CType(wrDelete.GetResponse(),  _

           FtpWebResponse)

 

        'Show Status Of Delete'

        Console.WriteLine("Delete status: {0}", _

           rDeleteResponse.StatusDescription)

 

        'Close'

        rDeleteResponse.Close()

    End Sub

 

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click

 

        'Create Request To Rename File'

        Dim wrRename As System.Net.FtpWebRequest = _

           CType(FtpWebRequest.Create("ftp://ftp.test.com/file.txt"),  _

           FtpWebRequest)

 

        'Specify Username & Password'

        wrRename.Credentials = New NetworkCredential("user", _

           "password")

 

        'Rename A File'

        wrRename.Method = WebRequestMethods.Ftp.Rename

 

        wrRename.RenameTo() = "TEST.TXT"

 

        'Determine Response Of Operation'

        Dim rResponse As System.Net.FtpWebResponse

 

        Try

            rResponse = CType(wrRename.GetResponse, FtpWebResponse)

 

            'Get Description'

            Dim strStatusDesc As String = rResponse.StatusDescription

 

            'Get Code'

            Dim strStatusCode As FtpStatusCode = rResponse.StatusCode

 

            If strStatusCode <> Net.FtpStatusCode.FileActionOK Then

 

                MessageBox.Show("Rename failed.  Returned status =                " & strStatusCode & " " & strStatusDesc)

 

            Else

 

                MessageBox.Show("Rename succeeded")

 

            End If

 

        Catch ex As Exception

 

            MessageBox.Show("Rename failed. " & ex.Message)

 

 

        End Try

    End Sub

End Class