获取Azure文件共享(CloudFileDirectory)中的所有文件

时间:2019-03-26 17:21:13

标签: c# azure-storage

我正在寻找一种C#方法,该方法将返回Azure文件共享中的所有文件(在所有子目录中)。

有一个示例,但会引发运行时错误。我已经尝试了下面的代码示例,但是从这里得到了异常。

我将代码粘贴到了这里,但是如果有人使用遍历Azure目录的方法,get文件将会很棒。

CloudFileDirectory dir = fclient.GetShareReference(share.ToString()).GetRootDirectoryReference();

foreach (IListFileItem file in dir.ListFilesAndDirectories())   //.Directory.ListFilesAndDirectories())
{
    list_subdir(file);
}   

和方法。

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());
        }
    }
}

”,位于Microsoft.WindowsAzure.Storage.Core.Executor.Executor.d__1 1.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()中    C:\ Program Files(x86)\ Jenkins \ workspace \ dotnet-split-pr-master \ Lib \ ClassLibraryCommon \ Core \ Executor \ Executor.cs:第41行

1 个答案:

答案 0 :(得分:1)

我安装了9.3.3版的nuget软件包WindowsAzure.Storage。下面的代码对我来说很好用,列出了子目录中的所有文件。

        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());
                }
            }

        }

文件共享中的目录:

根:

enter image description here

子目录1:

enter image description here

子目录2:

enter image description here

测试结果:列出了子目录中的所有文件:

enter image description here