The ReceiveFile and BeginReceiveFile/EndReceiveFile methods receive one file from an FTP server while the ReceiveMultipleFiles and BeginReceiveMultipleFiles/EndReceiveMultipleFiles methods receive one or more files.
AsyncFtpClient's methods now call the corresponding synchronous methods on a background thread. For this reason, the AsyncFtpClient class is now considered obsolete. It is therefore recommended to use FtpClient instead to improve code readability.
When using Xceed FTP for .NET in a WinForms application, it is recommended that a SynchronizingObject be assigned to the SynchronizingObject property of the FtpClient class. For more information, jump to theWinForms applications and threadingtopic.
Synchronous (blocking) and asynchronous (non-blocking) demonstration using FtpClient
The following example demonstrates how to receive a file from an FTP server using the ReceiveFile method and provide logging information during the process using the CommandSent and ReplyReceived events. An asynchronous (non-blocking) demonstration is also available.
VB.NET
Copy Code
Imports Xceed.Ftp
' When using FtpClient, you can instruct ' the library to automatically redirect events on the main UI thread ' by setting the SynchronizingObject property. ftp.SynchronizingObject = Me
Private Sub FTP_CommandSent(ByVal sender As Object, ByVal e As CommandSentEventArgs) ListBox1.Items.Add(">> " + e.Command) End Sub
Private Sub FTP_ReplyReceived(ByVal sender As Object, ByVal e As ReplyReceivedEventArgs) ListBox1.Items.Add("<< " + e.Reply.ToString()) End Sub
C#
Copy Code
using Xceed.Ftp;
FtpClient ftp = new FtpClient();
// When using FtpClient, you can instruct // the library to automatically redirect events on the main UI thread // by setting the SynchronizingObject property. ftp.SynchronizingObject = this;
ftp.CommandSent += new CommandSentEventHandler( this.FTP_CommandSent ); ftp.ReplyReceived += new ReplyReceivedEventHandler( this.FTP_ReplyReceived );
To clarify the code, instead of using callbacks, we will wait for completion of the operation before calling the matching "End" method. More information is available in the WinForms application and threading topic.
Do you want the FTP server to initiate the data connection rather than the FTP client? Set the PassiveTransfer property to false.
Do you want the file(s) to be received in ASCII mode rather than binary? Set the RepresentationType property to ASCII.
Do you want to decrease or increase the period of time after which an FTP operation should timeout? Change the value of the Timeout property.
Do you want to create a log file of the FTP process? Set the TraceWriter property.
Do you want to know the state of the FTP client? Check the Connected and Busy properties. You can also check the State property for specific state information. The StateChanged event can also be used to know when the state of the FTP client changes.
Do you want to know when a file is being received? Handle the ReceivingFile event.
Do you want to display progress information? Handle the FileTransferStatus event.
Do you want to continue a multiple-file transfer (when using the ReceiveMultipleFiles method) when one or more of the files being transferred causes an error? Handle the MultipleFileTransferError event.
Do you want to abort the FTP operation? Call the Abort method.
Do you want to prevent routers from prematurely closing the command channel while a long data transfer is taking place. Set the KeepAliveInterval property.