0 votes
by (120 points)

Is there a way for GetList() to return a list of only subdirectories (exclude any files) of the current directory I am making the call from I don't want to traverse any deeper. I solved this by traversing the list of items returned by my GetList() query and testing for item.IsDirectory()and if true, adding to a 2nd
SftpItemCollection list, which I then use. But I was interested in a more elegant, less redundant solution - ie; being able to tell the first GetList() call to return only directories and exclude files.

Applies to: Rebex SFTP

1 Answer

0 votes
by (132k points)

Unfortunately, the SFTP protocol does not offer any feature that would make the server only return the list of directories, excluding files.

But it's at least possible to implement this at the client side slightly more using Sftp object's ListItemReceived event, which is raised for each item received from the server, making it possible to remove it from the list returned by GetList() call:

// register listing handler
client.ListItemReceived += (s, e) =>
{
    if (!e.Item.IsDirectory)
    {
        e.Ignore();
    }
};

// perform listing
var list = client.GetList();

(Note: In real-world example, you would most likely want to move event handler code to a separate method to make it possible to unregister it after the GetList() call.)

...