Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

c# - Getting all files in Azure file share (CloudFileDirectory)

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...