BlobStorage.Advanced.ListBlobsHierarchicalListingAsync C# (CSharp) Method

ListBlobsHierarchicalListingAsync() private static method

Lists blobs in the specified container using a hierarchical listing, and calls this method recursively to return the contents of each virtual directory. Reads the properties on each blob or virtual directory returned and writes them to the console window.
private static ListBlobsHierarchicalListingAsync ( Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container, string prefix ) : System.Threading.Tasks.Task
container Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer A CloudBlobContainer object.
prefix string The blob prefix.
return System.Threading.Tasks.Task
        private static async Task ListBlobsHierarchicalListingAsync(CloudBlobContainer container, string prefix)
        {
            // List blobs in segments.
            Console.WriteLine("List blobs (hierarchical listing):");
            Console.WriteLine();

            // Enumerate the result segment returned.
            BlobContinuationToken continuationToken = null;
            BlobResultSegment resultSegment = null;

            try
            {
                // Call ListBlobsSegmentedAsync recursively and enumerate the result segment returned, while the continuation token is non-null.
                // When the continuation token is null, the last segment has been returned and execution can exit the loop.
                // Note that blob snapshots cannot be listed in a hierarchical listing operation.
                do
                {
                    resultSegment = await container.ListBlobsSegmentedAsync(prefix, false, BlobListingDetails.Metadata, null, null, null, null);

                    foreach (var blobItem in resultSegment.Results)
                    {
                        Console.WriteLine("************************************");
                        Console.WriteLine(blobItem.Uri);

                        // A hierarchical listing returns both virtual directories and blobs.
                        // Call recursively with the virtual directory prefix to enumerate the contents of each virtual directory.
                        if (blobItem is CloudBlobDirectory)
                        {
                            PrintVirtualDirectoryProperties((CloudBlobDirectory)blobItem);
                            CloudBlobDirectory dir = blobItem as CloudBlobDirectory;
                            await ListBlobsHierarchicalListingAsync(container, dir.Prefix);
                        }
                        else
                        {
                            // Write out blob properties and metadata.
                            PrintBlobPropertiesAndMetadata((CloudBlob)blobItem);
                        }
                    }

                    Console.WriteLine();

                    // Get the continuation token, if there are additional segments of results.
                    continuationToken = resultSegment.ContinuationToken;

                } while (continuationToken != null);
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }