我向您展示我的项目,该项目使FTP中的可用文件与我的机器上的文件夹同步。
这里的同步进行得很好,除了它不能用于文件夹...
似乎该文件夹在我的计算机上可以很好地自我复制,但是作为没有扩展名的文件而不是文件夹。
我的代码: 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