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.)