Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)Jenkinsworkspacedotnet-split-pr-masterLibClassLibraryCommonCoreExecutorExecutor.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_01.b__0() in C:Program Files (x86)Jenkinsworkspacedotnet-split-pr-masterLibClassLibraryCommonCoreExecutorExecutor.cs:line 41

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
246 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

enter image description here

sub-directory 1:

enter image description here

sub-directory 2:

enter image description here

Test result: all files in sub-directories are listed:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...