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.
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>();
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With