title: How to list all virtual directories and subdirectories using Azure Storage Client Library for .NET (BLOBS)
body: <p><strong>How to list ALL directories AND subdirectories in a blob container?</strong></p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public List&lt;CloudBlobDirectory&gt; Folders { get; set; }
  
  public List&lt;CloudBlobDirectory&gt; GetAllFoldersAndSubFoldersFromBlobStorageContainer()
  {
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
      CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
  
      if (container.Exists())
      {
          Folders = new List&lt;CloudBlobDirectory&gt;();
  
          foreach (var item in container.ListBlobs())
          {
              if (item is CloudBlobDirectory)
              {
                  var folder = (CloudBlobDirectory)item;
                  Folders.Add(folder);
                  GetSubFolders(folder);
              }
          }
      }
  
      return Folders;
  }
  
  private void GetSubFolders(CloudBlobDirectory folder)
  {
      foreach (var item in folder.ListBlobs())
      {
          if (item is CloudBlobDirectory)
          {
              var subfolder = (CloudBlobDirectory)item;
              Folders.Add(subfolder);
              GetSubFolders(subfolder);
          }
      }
  }
  </code></pre>
  
  <p>The above code-snippet gives me the list that I want but I am unsure about the recursive method and other .NET/C# syntax and best practice programming patterns. Simply put, I would like the end result to be as elegant and as performant as possible. </p>
  
  <p><strong>How can the above code-snippet be improved?</strong></p>