Imports System.Net
Imports System.IO

Namespace PCSolution

    Class FTP


        Public Sub Upload(ByVal source As String, ByVal target As String, ByVal credential As NetworkCredential)

            Dim request As FtpWebRequest = _
                  DirectCast(WebRequest.Create(target), FtpWebRequest)

            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = credential

            Dim reader As New FileStream(source, FileMode.Open)
            Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
            reader.Read(buffer, 0, buffer.Length)
            reader.Close()
            request.ContentLength = buffer.Length
            Dim stream As Stream = request.GetRequestStream
            stream.Write(buffer, 0, buffer.Length)
            stream.Close()
            Dim response As FtpWebResponse = DirectCast(request.GetResponse, FtpWebResponse)
            'MessageBox.Show(response.StatusDescription, "File Uploaded")
            response.Close()

        End Sub

        Public Sub MakeDirectory(ByVal target As String, ByVal credential As NetworkCredential)

            Dim request As FtpWebRequest = _
                  DirectCast(WebRequest.Create(target), FtpWebRequest)

            request.Method = WebRequestMethods.Ftp.MakeDirectory
            request.Credentials = credential
            Try
                request.GetResponse()
            Catch
            End Try

        End Sub

        Public Function FileExists(ByVal target As String, ByVal credential As NetworkCredential) As Boolean

            Dim request As FtpWebRequest = _
                  DirectCast(WebRequest.Create(target), FtpWebRequest)

            request.Credentials = credential
            request.Method = WebRequestMethods.Ftp.GetFileSize
            Try
                Dim response As FtpWebResponse = request.GetResponse()
                Return True
            Catch ex As WebException
                Dim response As FtpWebResponse = ex.Response
                If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
                    Return False
                End If
            End Try

            Return True

        End Function


    End Class

End Namespace