在c#中获取存储帐户文件共享中上传的所有文件

get all the files uploaded in storage account file share in c#

我想使用 c# 以编程方式获取存储帐户文件共享中上传的所有文件。

我尝试使用此代码

IEnumerable fileList = cloudFileShare.GetRootDirectoryReference().ListFilesAndDirectories();

但是它抛出错误 cloudfiledirectory' does not contain a definition for 'listfilesanddirectories'.

我正在尝试遍历文件共享并获取所有文件

此代码将获取 Azure 文件共享中所有文件夹中的所有文件。

using Azure.Storage.Files.Shares;
using System;
using System.IO;
using System.Threading.Tasks;

namespace GetAllStorageAccountFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var x = GetShareFilesAsync();
            x.Wait();
            Console.WriteLine("Done!");
            Console.ReadKey();
        }

        static async Task GetShareFilesAsync(string dirName = "")
        {
            string connectionString = "";

            // Name of the share, directory
            string shareName = "media";

            // Get a reference to a share
            ShareClient share = new ShareClient(connectionString, shareName);

            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            var files = directory.GetFilesAndDirectories();

            foreach (var file in files)
            {
                if (file.IsDirectory)
                {
                    Console.WriteLine("Folder: " + Path.Combine(dirName, file.Name));
                    GetShareFilesAsync(Path.Combine(dirName, file.Name));
                    
                }

                Console.WriteLine("File:" + Path.Combine(dirName, file.Name));
            }


        }

    }
}

根据请求也为 Blob 添加。

static async Task GetBlobFiles()
{


    string blobstorageconnection = "DefaultEndpointsProtocol=https;AccountName=stomyuploadpublic;AccountKey=oSM1+BTK8cKbax6dxslT5Gm1aO9AjoH3oRTl43RkK6ZdcrLWB0FVAwoba1CopPycS0Ng3voVu6UR59UMK7ytsg==;EndpointSuffix=core.windows.net";
    string blobContainer = "public";

    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);

    // Create the blob client.
    CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
    CloudBlobDirectory dirb = container.GetDirectoryReference(blobContainer);


    BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.Metadata, 500, null, null, null);

    foreach (var blobItem in resultSegment.Results)
    {
        var blob = (CloudBlob)blobItem;
        Console.WriteLine(blob.Name);

    }

}