1

I present to you my project, which makes synchronize the available files from an FTP and a folder on my amchine.

Here synchronization goes very well, except that it does not work for folders ...

it seems that the folder copies itself well on my machine but as a file without extension instead of a folder.

My Code : Form1.vb

Imports System.ComponentModel
Imports System.Net

Public Class Form1
    Private LocalPath As String = "SYNC-FOLDER"
    Private missingFiles As New List(Of String)


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    FTPManager.loadConfig()
    createLocalFolderIfNotExistes()
    btnRefresh.performClick()

End Sub

Private Sub createLocalFolderIfNotExistes()
    If Not IO.Directory.Exists(LocalPath) Then IO.Directory.CreateDirectory(LocalPath)
End Sub

Private Sub getLocalFiles()
    dgvLocal.Rows.Clear()
    Dim files = IO.Directory.GetFiles(LocalPath)
    If files.Count > 0 Then
        For Each f In files
            Me.dgvLocal.Rows.Add(f.Split("\c").Last())

        Next
    End If
End Sub

Private Sub getFTPFiles()
    dgvFTP.Rows.Clear()
    missingFiles.Clear()
    Dim request = FtpWebRequest.Create(FTPManager.ServerRootPatch)
    request.Method = WebRequestMethods.Ftp.ListDirectory
    request.Credentials = New NetworkCredential(FTPManager.User, FTPManager.PW)
    Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)

    Using myReader As New IO.StreamReader(response.GetResponseStream())
        Do While Not myReader.EndOfStream
            Dim file = myReader.ReadLine()
            Me.dgvFTP.Rows.Add(file)

            If Not IO.File.Exists(LocalPath & "\" & file) Then
                dgvFTP.Rows(dgvFTP.Rows.Count - 1).DefaultCellStyle.BackColor = Color.FromArgb(255, 192, 192)
                missingFiles.Add(file)
            Else : dgvFTP.Rows(dgvFTP.Rows.Count - 1).DefaultCellStyle.BackColor = Color.FromArgb(192, 255, 192)
            End If
        Loop
    End Using
End Sub

Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
    If btnRefresh.Enabled = True Then
        '  btnRefresh.Enabled = False
        Task.Delay(2000)
        getLocalFiles()
        getFTPFiles()
        btnSync.Enabled = True
    Else : MessageBox.Show("please wait")

    End If
    dgvFTP.ClearSelection()
    dgvLocal.ClearSelection()

End Sub

Private Sub btnSync_Click(sender As Object, e As EventArgs) Handles btnSync.Click
    If missingFiles.Count > 0 Then
        Donwload_File(New Uri(FTPManager.ServerRootPatch & FTPManager.Folder & "/" & missingFiles(0)))
    Else : MessageBox.Show("No files to downlaod")
    End If
End Sub

Private Sub Donwload_File(URI As Uri)
    Dim filename = URI.ToString().Split("/c").Last()
    prgb.Value = 0
    lblFile.text = filename
    Using wc As New WebClient
        wc.Credentials = New NetworkCredential(FTPManager.User, FTPManager.PW)
        AddHandler wc.DownloadProgressChanged, AddressOf File_DLProgressChanged
        AddHandler wc.DownloadFileCompleted, AddressOf File_Downloaded
        wc.DownloadFileAsync(URI, LocalPath & "\" & filename)
    End Using
End Sub

Private Sub File_DLProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
    prgb.Value = e.ProgressPercentage
End Sub

Private Sub File_Downloaded(sender As Object, e As AsyncCompletedEventArgs)
    Dim deletedFile = missingFiles(0)
    missingFiles.RemoveAt(0)

    If missingFiles.Count > 0 Then
        Donwload_File(New Uri(FTPManager.ServerRootPatch & FTPManager.Folder & "/" & missingFiles(0)))
    Else
        btnRefresh.PerformClick()
        MessageBox.Show("niquel")
    End If
End Sub

Private Sub dgvLocal_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
End Class
Mary
  • 14,926
  • 3
  • 18
  • 27
  • My first two questions are resolved here I have an all other problem it is a member who told me to create a new question ... – Kersteens Colin Feb 24 '19 at 20:21
  • Sorry, I do not understand your comment. – Martin Prikryl Feb 24 '19 at 20:46
  • This is two error differently , my english is very bad.... – Kersteens Colin Feb 24 '19 at 21:28
  • What error is different? What you probably miss is that your code can never download folders. There's no "simple fix". It's fundamentally wrong. you cannot download folders. You can download only files. Do "download folder", you need to recurse into them, downloading individual files. That's what the linked answer shows. – Martin Prikryl Feb 24 '19 at 21:36

0 Answers0