I'm looking for a C# method that will return all files (in all sub-directories) in an Azure file share.
Have an example but it throws a run time error. I've tried the code example below which I got from here however it throws an exception.
I have pasted the code in here but if anyone has a method that walks the entrire Azure directory get gets files that would be great.
CloudFileDirectory dir = fclient.GetShareReference(share.ToString()).GetRootDirectoryReference();
foreach (IListFileItem file in dir.ListFilesAndDirectories()) //.Directory.ListFilesAndDirectories())
{
list_subdir(file);
}
And the method.
public static void list_subdir(IListFileItem list)
{
Console.WriteLine("subdir");
CloudFileDirectory fileDirectory = (CloudFileDirectory)list;
IEnumerable<IListFileItem> fileList = fileDirectory.ListFilesAndDirectories();
foreach (IListFileItem listItem in fileList)
{
if (listItem.GetType() == typeof(Microsoft.WindowsAzure.Storage.File.CloudFileDirectory))
{
list_subdir(listItem);
}
else
{
Console.WriteLine(listItem.Uri.Segments.Last());
}
}
}
" at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.d__11.MoveNext()
in C:\Program Files (x86)\Jenkins\workspace\dotnet-split-pr-master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.<>c__DisplayClass0_0
1.b__0() in
C:\Program Files (x86)\Jenkins\workspace\dotnet-split-pr-master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 41
I have the nuget package WindowsAzure.Storage, version 9.3.3 installed. And the code below works fine for me, all files in sub-directories are listed.
static void Main(string[] args)
{
string accountName = "xxx";
string key = "xxxx";
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
var share = storageAccount.CreateCloudFileClient().GetShareReference("testfolder");
IEnumerable<IListFileItem> fileList = share.GetRootDirectoryReference().ListFilesAndDirectories();
foreach (IListFileItem listItem in fileList)
{
if (listItem.GetType() == typeof(CloudFile))
{
Console.WriteLine(listItem.Uri.Segments.Last());
}
else if(listItem.GetType() == typeof(CloudFileDirectory))
{
list_subdir(listItem);
}
}
Console.WriteLine("done now");
Console.ReadLine();
}
public static void list_subdir(IListFileItem list)
{
//Console.WriteLine("subdir");
CloudFileDirectory fileDirectory = (CloudFileDirectory)list;
IEnumerable<IListFileItem> fileList = fileDirectory.ListFilesAndDirectories();
foreach (IListFileItem listItem in fileList)
{
if (listItem.GetType() == typeof(CloudFileDirectory))
{
list_subdir(listItem);
}
else
{
Console.WriteLine(listItem.Uri.Segments.Last());
}
}
}
The directories in file share:
root:
sub-directory 1:
sub-directory 2:
Test result: all files in sub-directories are listed: