Click here to Skip to main content
15,915,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Could someone help me to check whether my code is valid or not?I just make some code for uploading file into FTP server(my friend's PC:192.168.1.62). We connected by proxy server (eg: 192.168.1.1). Before I upload file into FTP, I need to check whether a specific folder is existed in FTP. If not, it will create one. Please check and correct me if my code is wrong..The file is a bunch of an Images.jpg which define it's name by date and time

VB
Private Sub getFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getFileName.Click

      Dim source As String = "C:\FTP\Backup\"
      Dim i As Integer
      Dim strFTPCollNo As String = String.Empty
      Dim strFTPDate As String = String.Empty
      Dim getFileName As String() = Directory.GetFiles(source, "*.jpg")
      Dim strFileName As String
      Dim strFilePath As String
      Dim FTPUsername As String = "FTPName"
      Dim FTPPassword As String = "FTPPwd"
      Dim FTPServerName As String = "192.168.1.62"
      Dim DestFile As String = "//192.168.1.62/c$/C:/FTPFolder/"
      Dim strProxy As String = "192.168.1.1"

   For i = 0 To getFileName.Length
      strFilePath = getFileName(i)
      strFileName = Path.GetFileName(getFileName(i))
      strFTPDate = Format(Date.Today,"ddMMyyyy")
      DestFile = DestFile & strFTPDate
      TryCheckFTPServer(strFilePath, DestFile, FTPUsername, FTPPassword, strProxy)
   Next
End Sub
---------------------------------------------------------------------------------
Private Sub TryCheckFTPServer(ByVal strSourceFile As String, ByVal strDestFile As String, ByVal strUserID As String, ByVal strPassword As String, ByVal strProxy As String)

     Dim buffer() As Byte
     Dim stream As FileStream
     Dim reqStream As Stream
     Dim request As FtpWebRequest = WebRequest.Create(DestFile)

     request.Credentials = New NetworkCredential(FTPUsername, FTPPassword)
     request.Method = WebRequestMethods.Ftp.ListDirectory
     request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory

Try
     Dim response As FtpWebResponse = request.GetResponse()
     request.KeepAlive = False
     request.UseBinary = True
     request.Method = WebRequestMethods.Ftp.UploadFile
     request.Credentials = New NetworkCredential(strUserID, strPassword)
     request.UsePassive = True
     request.UseBinary = True
     request.KeepAlive = False


     If strProxy = String.Empty Then
         webReq.Proxy = Nothing
     Else
         webReq.Proxy = strProxy.Trim
     End If

     stream = File.OpenRead(strSourceFile)
     ReDim buffer(stream.Length)

     stream.Read(buffer, 0, buffer.Length)
     stream.Close()

     reqStream = webReq.GetRequestStream
     reqStream.Write(buffer, 0, buffer.Length)
     reqStream.Close()

Catch ex As WebException
     Dim response As FtpWebResponse = ex.Response
     If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
        CreateFTPServer(strSourceFile, strDestFile, strUserID, strPassword, strProxy)
     End If
End Try
End Sub
---------------------------------------------------------------------------------
Private SubCreateFTPServer(ByVal strSourceFile As String, ByVal strDestFile As String, ByVal strUserID As String, ByVal strPassword As String, ByVal strProxy As String) As Boolean

    Dim file As Byte()
    Dim req As FtpWebRequest = WebRequest.Create(strDestFile)
    Dim credentials As New System.Net.NetworkCredential(strUserID, strPassword)

    Try
        webReq = FtpWebRequest.Create(strDestFile)
        webReq.Method = WebRequestMethods.Ftp.UploadFile
        webReq.Credentials = New NetworkCredential(strUserID, strPassword)
        webReq.UsePassive = True
        webReq.UseBinary = True
        webReq.KeepAlive = False

        If strProxy = String.Empty Then
            webReq.Proxy = Nothing
        Else
            webReq.Proxy = strProxy.Trim
        End If

        stream = File.OpenRead(strSourceFile)
        ReDim buffer(stream.Length)

        stream.Read(buffer, 0, buffer.Length)
        stream.Close()

        reqStream = webReq.GetRequestStream
        reqStream.Write(buffer, 0, buffer.Length)
        reqStream.Close()
    Catch ex As Exception

    End Try
End Sub


Someone please help me
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900