【问题标题】:How to list all the Directories or Files, from a root Directory, in Azure File Storage -and- using the .NET Storage SDK?如何使用 .NET 存储 SDK 在 Azure 文件存储中列出根目录中的所有目录或文件?
【发布时间】:2020-07-22 09:24:56
【问题描述】:

我在想办法

  • 列出单个目录下的所有目录

-或-

  • 列出单个目录中的所有文件

使用Azure Storage Client Library for .NET

MS docs do give us an API endpoint to hit ...但我希望使用 .NET Storage SDK 为我处理大量低级通信(身份验证等)。

谁能告诉我这样做需要什么?

【问题讨论】:

    标签: c# azure azure-storage


    【解决方案1】:

    请使用look at our File storage sample 获取一些显示如何执行此操作的代码。

    片段:

    //***** Get list of all files/directories on the file share*****//
    
    // List all files/directories under the root directory.
    Console.WriteLine("Getting list of all files/directories under the root directory of the share.");
    
    IEnumerable<IListFileItem> fileList = cloudFileShare.GetRootDirectoryReference().ListFilesAndDirectories();
    
    // Print all files/directories listed above.
    foreach (IListFileItem listItem in fileList)
    {
        // listItem type will be CloudFile or CloudFileDirectory.
        Console.WriteLine("    - {0} (type: {1})", listItem.Uri, listItem.GetType());
    }
    
    Console.WriteLine("Getting list of all files/directories in the file directory on the share.");
    

    【讨论】:

    • ListFilesAndDirectories 被移除了吗?
    • 我知道它已被接受,但并没有真正回答“目录或文件”的问题,它列出了目录和文件。这不是迂腐的,例如我正在寻找一种仅列出目录中文件的方法。为什么没有 ListFiles?甚至 IListFileItem 也不会告诉您它是文件还是目录。此外(也许更迂腐)是它从“a”根文件夹中询问,而这只是显示“the”根文件夹。在处理文件存储时,这可能是一个有用的区别。
    【解决方案2】:
    var directories = fileDirectory.ListFilesAndDirectories().Where(x => x.GetType() == 
    typeof(CloudFileDirectory)).Cast<CloudFileDirectory>();
    
    var files = fileDirectory.ListFilesAndDirectories().Where(x => x.GetType() == 
    typeof(CloudFile)).Cast<CloudFile>().ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      相关资源
      最近更新 更多