0 votes
by (8.6k points)
edited

I'm trying to get a list of files from a FTP or SFTP server. The result have to be ordered. How do you sort a result of GetFiles method of Rebex FTP? I need to show results ordered by file name.

Applies to: Rebex FTP/SSL, Rebex SFTP

1 Answer

0 votes
by (13.3k points)
edited

If you are using .NET 3.5 or newer you can use LINQ to sort this collection. Check following code:

using (Ftp ftp = new Ftp())
{
       ftp.Connect("host");
       ftp.Login("username", "password");

       FtpList items = ftp.GetList();

       IEnumerable<FtpItem> sortedItems =
             items.OrderByDescending(n => n.Name);

       foreach (FtpItem item in sortedItems)
       {
             Console.WriteLine("{0}", item.Name);
       }

       ftp.Disconnect();
}

If you cannot use LINQ try this code:

using (Ftp ftp = new Ftp())
{
       ftp.Connect("hostname");
       ftp.Login("username", "password");

       FtpList items = ftp.GetList();

       items.Sort(new FtpItemComparer(FtpItemComparerType.Name));

       foreach (FtpItem item in items)
       {
             Console.WriteLine("{0}", item.Name);
       }

       ftp.Disconnect();
}

The code for SFTP is almost the same. Just replace FtpList with SftpList, Ftp with Sftp and FtpItemComparer with SftpItemComparer.

...