ListItemReceivedEventArgs Item.Path not containing full path

0 votes
asked Aug 16 by rexh (450 points)

Is this a bug that the e.Item.Path does not contain the full path?
On the other hand, lst[0].Path has the full path.

   ftp.ListItemReceived += OnItemReceived;
   var lst = await ftp.GetItemsAsync(remote, TraversalMode.Recursive);
   var path = lst[0].Path;
}
void OnItemReceived(object s, ListItemReceivedEventArgs e)
{
   var path = e.Item.Path;
}
Applies to: Rebex FTP/SSL

1 Answer

0 votes
answered Aug 17 by Lukas Matyska (57,170 points)
selected Aug 17 by rexh
 
Best answer

It is known behavior.

The GetItems() method uses the GetList() method internally to achieve complex tasks such as recursive directory traversing.

The ListItemReceived is fired from the GetList() method, which does not know the context of the complex operation.

To fill the correct full path in the event we would pass the context to the GetList() method. However, you can do the same easily like this:

var client = new Ftp();
client.Connect("test.rebex.net");
client.Login("demo", "password");

string basePath = "/pub/example/";

client.ListItemReceived += (s, e) =>
{
    var fullPath = basePath + e.Item.Name;
    Console.WriteLine("{0}", fullPath);
};

var items = client.GetItems(basePath + "*", TraversalMode.NonRecursive);

Since you are using GetItemsAsync() method, you can even pass the original full path to the UserState:

client.ListItemReceived += (s, e) =>
{
    var fullPath = (string)e.UserState + e.Item.Name;
    Console.WriteLine("{0}", fullPath);
};

string basePath = "/pub/example/";
var items = await client.GetItemsAsync(basePath + "*", TraversalMode.NonRecursive, basePath);
...