Search code examples
c#azureazure-storagefile-storage

How do we track azure file storage folder is updated or not?


I have azure file storage with other folders inside it. I want to check or track when that folder is last updated ?

I have done a simple console application to get all files from that location -

        // 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"]));
        IEnumerable<IListFileItem> fileList = sourceName.ListFilesAndDirectories();

        CloudFileDirectory destinationDir = fileShare.GetRootDirectoryReference().GetDirectoryReference((ConfigurationManager.AppSettings["destinationName"]));

        foreach (IListFileItem listItem in fileList)
        {
         // gives me all file records 
        }

I tried to get it like this but it is null. var test = sourceName.Properties.LastModified;

because of null i can not able to use this query :(

 var latest= fileShare.GetRootDirectoryReference().ListFilesAndDirectories()
    .OfType<CloudFileShare>()
    .OrderByDescending(m => m.Properties.LastModified)
    .ToList()
    .First();

i just noticed that , i have uploaded one new file just now into that folder but still LastModified is old date , it means LastModified is separate for that folder and file :O what to do now ?

I want to check whether any new file is updated into any sub-fodler of main folder or not within 24hr.

I wonder how to I check the last updated file datetime from that source folder ?

why LastModified is null ?


Solution

  • To retrieve property values, call the FetchAttributesAsync method on your blob or container to populate the properties, then read the values.

    When you run your console app, it essentially creates a new instance of CloudFileShare object and its properties are initialized to the default value. You would need to call FetchAttributes method on this to fill the properties.

    Also, when you list the file's the properties of the file are fetched as well, you will not need to create a new instance of CloudFileShare. You could refer to this thread.

    You could use sourceName.FetchAttributes(); before retrieve property. I test it and it works fine. Please refer to the code as below:

    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"]));
    sourceName.FetchAttributes();
    var test = sourceName.Properties.LastModified;