Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Download Remote FTP Directory TreeDownload: Chilkat .NET Assemblies VB.NET sample code to download an entire directory tree from an FTP server, recreating it on the local filesystem. ' Download an entire directory tree from an FTP server,
' recreating it on the local filesystem.
'
' The download is abortable at any point via the AbortCheck event callback.
'
' Three other event callbacks are available:
' OnBeginDownloadFile - called before each file is downloaded, allowing
' your application to skip specific files.
' OnEndDownloadFile - called after each file is downloaded, allowing
' your application to log the files and sizes of files downloaded.
' OnVerifyDownloadDir - called before each remote sub-directory is entered,
' allowing your application to skip entire sub-directories.
' Don't forget to use WithEvents!
Dim WithEvents ftpObj As New Chilkat.Ftp2
Dim AbortButtonPressed As Boolean
' This is the OnAbortCheck event callback. It is called periodically according
' to the HeartbeatMs property setting.
Private Sub ftpObj_OnAbortCheck(ByVal sender As Object, ByVal args As Chilkat.AbortCheckEventArgs) Handles ftpObj.OnAbortCheck
' Was the Abort button pressed?
If (AbortButtonPressed) Then
' To abort the current FTP operation, set Abort = true
args.Abort = True
End If
' Update a progress bar so we can visually see the heartbeat:
If (ProgressBar1.Value < 100) Then
ProgressBar1.Value = ProgressBar1.Value + 10
Else
ProgressBar1.Value = 0
End If
' Handle application events so our user interface remains responsive.
Application.DoEvents()
End Sub
' Our user-interface has an abort button which when pressed,
' sets the AbortCurrentFtpOp boolean to True. If an FTP operation
' is in progress, it will be aborted at the next heartbeat.
Private Sub AbortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortButton.Click
AbortButtonPressed = True
End Sub
' Called before each file is downloaded:
Private Sub ftpObj_OnBeginDownloadFile(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnBeginDownloadFile
' In this example, we will skip any file ending with .obj
If (args.Path.EndsWith(".obj")) Then
args.Skip = True
End If
End Sub
' Called after each file is downloaded.
Private Sub ftpObj_OnEndDownloadFile(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnEndDownloadFile
' Add the local filepath and size of file to a listbox.
ListBox1.Items.Add(args.Path & ": " & Convert.ToString(args.NumBytes))
ListBox1.Refresh()
End Sub
' Called before each remote sub-directory is entered.
Private Sub ftpObj_OnVerifyDownloadDir(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnVerifyDownloadDir
ListBox1.Items.Add("DIR: " + args.Path)
ListBox1.Refresh()
' Skip all directories having "temp" in the path
If (args.Path.Contains("/temp/")) Then
args.Skip = True
End If
End Sub
' OK, now for the code to download an entire tree:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim success As Boolean
success = ftpObj.UnlockComponent("Anything for 30-day trial")
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' -----------------------------------------------
' IMPORTANT: Be sure to enable events on the object
' -----------------------------------------------
ftpObj.EnableEvents = True
' Set our heartbeat interval to .1 seconds:
ftpObj.HeartbeatMs = 100
' Set the FTP hostname, login, and password.
ftpObj.Hostname = "ftp.yourftpserver.com"
ftpObj.Username = "***"
ftpObj.Password = "***"
' Connect and login to the FTP server. Heartbeat events fire during the
' the connection establishment, authenticaion, and during all other communications
' with the FTP server.
success = ftpObj.Connect()
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Navigate to the root of the remote directory tree to be downloaded.
success = ftpObj.ChangeRemoteDir("b")
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Download the entire directory tree into c:/temp/b
success = ftpObj.DownloadTree("c:/temp/b")
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Disconnect
ftpObj.Disconnect()
' We're finished!
MessageBox.Show("FTP DownloadTree Finished!")
End Sub
|
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.