Downloading multiple files and directories

Download multiple files and directories with ATP UltimateFtp is really simple. You only need to call the DownloadFiles method with few parameters. The component will do the rest of hard work for you. It will loop through the specified directory, find files that match the specified search criteria, create threads, and start downloading. The steps belows show you how to use Ultimate FTP to download files with three threads.

Downloading files using multiple threads

Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:

C# Copy Code
using Atp.Net;
using Atp.IO;
VB.NET Copy Code
Imports Atp.Net
Imports Atp.IO

Create a new instance of the Ftp class.
C# Copy Code
// Create a new instance.
Ftp client = new Ftp();
VB.NET Copy Code
' Create a new instance.
Dim client As New Ftp()
Register event handlers to the ThreadCompleted and ThreadsCompleted events to get informed when a thread has completed and all threads have completed. The code looks similar to the following:
C# Copy Code
client.ThreadCompleted += client_ThreadCompleted;
client.ThreadsCompleted += client_ThreadsCompleted;
VB.NET Copy Code
AddHandler client.ThreadCompleted, AddressOf client_ThreadCompleted
AddHandler client.ThreadsCompleted, AddressOf client_ThreadsCompleted

Now pass all needed parameters to the DownloadFiles method. The code looks similar to the following:
C# Copy Code
// Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
client.DownloadFiles("/my folder", "c:\\my folder", 3, true);
VB.NET Copy Code
' Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
client.DownloadFiles("/my folder""c:\my folder", 3, True)

Final example code

C# Copy Code
public void DoMultiThreadsDownloadFiles()
{
   
// Create a new instance of the Atp.Net.Ftp class.
   
Ftp client = new Ftp();
   
// Connect to the server.
   
client.Connect("server");
   
// Authenticate.
   
client.Authenticate("user", "pass");
   client.CommandResponse += client_ResponseRead;
   client.ThreadCompleted += client_ThreadCompleted;
   client.ThreadsCompleted += client_ThreadsCompleted;
   
// ...
   
// Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
   
client.DownloadFiles("/my folder", "c:\\my folder", 3, true);
   
// ...
   
client.Disconnect();
}
void client_ThreadsCompleted(object sender, ThreadsCompletedEventArgs e)
{
   Console.WriteLine(
"Multi-threads file transfer completed");
}
void client_ThreadCompleted(object sender, ThreadCompletedEventArgs e)
{
   Console.WriteLine(
string.Format("Thread ID {0} completed", e.FileSystem.ThreadId));
}
void client_ResponseRead(object sender, CommandResponseEventArgs e)
{
   Ftp client = (Ftp)sender;
   
if (client.ThreadId >= 0)
       
if (e.Command != null)
           Console.WriteLine(
"Thread: {0} - CMD>       {1}", client.ThreadId,
               e.Command);
       
else
           
Console.WriteLine("Thread: {0} - RESPONSE>  {1}", client.ThreadId,
               e.Response);
}
VB.NET Copy Code
Public Sub DoMultiThreadsDownloadFiles()
    ' Create a new instance.
    Dim client As New Ftp()
    ' Connect to the server.
    client.Connect("server")
    ' Authenticate.
    client.Authenticate("user""pass")
    AddHandler client.CommandResponse, AddressOf client_ResponseRead
    AddHandler client.ThreadCompleted, AddressOf client_ThreadCompleted
    AddHandler client.ThreadsCompleted, AddressOf client_ThreadsCompleted
    ' ...
    ' Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
    client.DownloadFiles("/my folder""c:\my folder", 3, True)
    ' ...
    client.Disconnect()
End Sub
Private Sub client_ThreadsCompleted(ByVal sender As ObjectByVal e AsThreadsCompletedEventArgs)
    Console.WriteLine("Multi-threads file transfer completed")
End Sub
Private Sub client_ThreadCompleted(ByVal sender As ObjectByVal e AsThreadCompletedEventArgs)
    Console.WriteLine(String.Format("Thread ID {0} completed", e.FileSystem.ThreadId))
End Sub
Private Sub client_ResponseRead(ByVal sender As ObjectByVal e AsCommandResponseEventArgs)
    Dim client As Ftp = CType(sender, Ftp)
    If client.ThreadId >= 0 Then
        If e.Command IsNot Nothing Then
            Console.WriteLine("Thread: {0} - CMD> {1}", client.ThreadId, e.Command)
        Else
            Console.WriteLine("Thread: {0} - RESPONSE> {1}", client.ThreadId, e.Response)
        End If
    End If
End Sub

You may want to see other topics:

Uploading selected files and directories

 

Click here to download the Ultimate FTP Component for .NET, or here to download the .NET CF version.