Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get all the files uploaded in the storage account file share programatically using c#.

I tried to use this code

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

But it is throwing an error cloudfiledirectory' does not contain a definition for 'listfilesanddirectories'.

I am trying to loop through file share and get all the files

like image 458
Bad_Coder Avatar asked Oct 11 '25 17:10

Bad_Coder


1 Answers

This code will get all files in all folders in Azure Fileshare.

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


        }

    }
}

As per request adding for Blob too.

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

    }

}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!