title: System.InvalidCastException in Renaming Azure Container
body: <p>I am trying to rename a container in azure blob storage. I was able to successfully rename the container. But I noticed in some few cases that during some process. I encountered some error.</p>
  
  <p>Here is the error message.</p>
  
  <blockquote>
    <p>System.InvalidCastException:  'Unable to cast object of type
    'Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory' to type
    'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob'.'</p>
  </blockquote>
  
  <p>Below is my code.</p>
  
  <pre><code>string ContainerName = "old-container-name";
      string NewContainerName = "new-container-name";
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
      CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
      CloudBlobContainer destcontainer = blobClient.GetContainerReference(NewContainerName);
      destcontainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
      IEnumerable&lt;IListBlobItem&gt; IE = container.ListBlobs(blobListingDetails: BlobListingDetails.Metadata);
      foreach (IListBlobItem item in IE)
      {
          CloudBlockBlob blob = (CloudBlockBlob)item;
          CloudBlockBlob destBlob = destcontainer.GetBlockBlobReference(blob.Name);
          destBlob.StartCopyAsync(new Uri(GetSharedAccessUri(blob.Name, container)));
      }
  </code></pre>
  
  <p>I received the error on this line:</p>
  
  <pre><code>CloudBlockBlob blob = (CloudBlockBlob)item;
  </code></pre>
  
  <p>Do you guys have a fix on this one? Any tips on how to fix this?</p>