Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AzureStorage CloudFileDirectory.ListFilesAndDirectories worthless?

So far I get a handle on my CloudFileDirectory and calling this method gives me a list of IListFileItem objects, 1 for each file and directory in the given CloudFileDirectory, but I'm having trouble distinguishing which are files and which are directories.

Properties available on the IListFileItem include Uri, which is a System.Uri object, which has an isFile member, but this shows as false for my files and my directories.

I need to get lists of directories which I can drill into, lists of files I can work with, but so far I can't find a way to filter this mixed up list.

I can't think of a worthwhile use for a mixed list. I wonder why M$ didn't expose ListFiles and ListDirectories as separate methods. Maybe they had trouble distinguishing them and gave up.

like image 480
Brian Lowe Avatar asked Jul 10 '15 11:07

Brian Lowe


2 Answers

I found the answer here... RobinDotNet's blog Using the Azure Files preview with the Storage Client Library

Casting the received IListFileItem as CloudFile only works for files, otherwise resulting in null, and casting it as CloudFileDirectory only works for directories.

I ended up using Linq...

var azureDirectories = myShare.GetRootDirectoryReference()
    .GetDirectoryReference(myPath)
    .ListFilesAndDirectories()
    .OfType<CloudFileDirectory>();

var azureFiles = myShare.GetRootDirectoryReference()
    .GetDirectoryReference(myPath)
    .ListFilesAndDirectories()
    .OfType<CloudFile>();
like image 67
Brian Lowe Avatar answered Oct 11 '22 13:10

Brian Lowe


To prevent listing the directory twice you could also do:

CloudFileDirectory rootDir = myShare.GetRootDirectoryReference();
// Get directory items once
IEnumerable<IListFileItem> azureItems = rootDir.ListFilesAndDirectories();
CloudFileDirectory azureDirectories = azureItems.OfType<CloudFileDirectory>();
CloudFile azureFiles = azureItems.OfType<CloudFile>();
like image 22
parphane Avatar answered Oct 11 '22 12:10

parphane