Search code examples
c#azureazure-files

null value for Azure File Share property LastModified


  // Get list of all files/directories on the file share 
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["storageConnectionString"]);
            CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient();
            CloudFileShare fileShare = fileClient.GetShareReference(ConfigurationManager.AppSettings["shareName"]);

            var sourceName = fileShare.GetRootDirectoryReference().GetDirectoryReference((ConfigurationManager.AppSettings["sourceName"]));

            var test = sourceName.Properties.LastModified;

But sourceName.Properties.LastModified is null

even fileShare.Properties.LastModified is null

I'm getting null when I'm trying to fetch LastModified property of Azure File Share.


Solution

  • The reason is every time you call GetRootDirectoryReference() you create a new instance of FileItem, causing in it's properties to be initialized to it's default value, just the same behavior as when using GetBlockBlobReference().

    What you need to do is to call FetchAttributes on this in order to fill all the properties.

    Note that when the properties are fetched you do not need to create a new instance of your object.

    you can follow this post that references blobs but is concerning your error as well.


    An example as you requested would be:

    public static void ListContainerMetadataAsync(CloudBlobContainer container)
    {
        // Fetch container attributes in order to populate the container's 
           properties and metadata.
         container.FetchAttributes();
    
        // Enumerate the container's metadata.
        Console.WriteLine("Container metadata:");
        foreach (var metadataItem in container.Metadata)
        {
            Console.WriteLine("\tKey: {0}", metadataItem.Key);
            Console.WriteLine("\tValue: {0}", metadataItem.Value);
        }
    }
    

    You can read more at docs.microsoft.