+2 votes

Hey there,

i need to list the full content of a ftp-server including dirs and subdirs. i want to add them to a datatable with filename, path and size in order to display all files and download them afterwards.

i successfully tried with a sample, but therefore it creates up to 4 connections if i try to run it recursively.
so there is a deletefolderrecursively but no listfilesrecursively..

by (510 points)

1 Answer

+1 vote
 
Best answer

Using more than one connections is certainly not a good option. You should be using search method:

using (Ftp ftp = new Ftp())
{
    ftp.Connect("ftp.example.com"); // or ConnectSSL
    ftp.Login("user","password");

    // RemoteSearchOptions options = new RemoteSearchOptions("*.txt", true);
    RemoteSearchOptions options = new RemoteSearchOptions()

    List<RemoteSearchItem> items = ftp.Search(options);
    foreach (RemoteSearchItem item in items)
    {
        string remotePath = item.GetRemotePath(); // full path e.g.: folder/a.txt
        string name = item.FtpItem.Name; // name e.g.: a.txt
        bool isFolder = item.IsFolder;            
    }

    ftp.Close();

}

You can download list of RemoteSearchItem instances from the FTP easily:

Directory.CreateDirectory("c:\\Downloads");
ftp.DownloadFiles("c:\\Downloads", items);

or you can use overloaded Download method to specify FTP search criteria in a single place:

using (Ftp ftp = new Ftp())
{
    ftp.Connect("ftp.example.com");    // or ConnectSSL

    Directory.CreateDirectory(@"c:\Downloads");

    // RemoteSearchOptions options = new RemoteSearchOptions("*.txt", true);
    RemoteSearchOptions options = new RemoteSearchOptions()         

    ftp.DownloadFiles(@"c:\Downloads", options);

    ftp.Close();
}
by (297k points)
selected by
aaaaawesome.... thank you so much! listing comes down from about 6 minutes to 30 seconds!
is there a way to create a remotesearchitem?
the problem is, i need to list all files in the first step, then filter files or let the user decide what files he needs and then the applications downloads the new selection so i cannot use the remotsearchoptions. download is one by one, very slow.  i need sth like the queue in filezilla ftp client.

Main question: what is the fastest way to download several known files (with known i mean, the remotepath and filename is known) at once?
You can't create RemoteSearchItem by yourself.
There is an overload that takes list of RemoteSearchItem:
DownloadFiles(string localFolderPath, List<RemoteSearchItem> remote)
You can use that to download only files specified by the user.

All download methods download files one-by-one. Unfortunately this is how FTP protocol was designed. No FTP client can do it better.
...