2

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 ?

Neo
  • 15,491
  • 59
  • 215
  • 405

1 Answers1

1

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;
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • thanks a lot this is only giving me `LastModified` for `sourcename` i want to know if sourcename is having any subfolder which is recently updated then overall LastModified for `sourcename` should be its sub folder value. for example - sourcename is having file LastModified at 11 Aug and child folder which is inside `suourcename` is modified at 16aug then overall main folder value is 16aug – Neo Aug 27 '18 at 10:28
  • 1
    Yes, it will be the latest modified data. – Joey Cai Aug 27 '18 at 10:32
  • ohh 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 ? – Neo Aug 27 '18 at 10:35
  • I want to check whether any new file is updated into any sub-fodler of main folder or not within 24hr. – Neo Aug 27 '18 at 10:37
  • can we use any linq query like i mentioned into question ? – Neo Aug 27 '18 at 10:39
  • 1
    You could use `DateTime dt = File.GetLastWriteTime(path);` to get the modify data. And traverse all your file in your folder. Here is a similar [case](https://stackoverflow.com/questions/30185851/get-all-files-in-directory-and-subdirectory-after-date-range). – Joey Cai Aug 27 '18 at 10:42
  • ok but in path i have Uri and it wont work here :( its azure storage – Neo Aug 27 '18 at 10:47
  • any linq query link you can suggest which will check for the latestModified property u sub-folders? – Neo Aug 27 '18 at 10:50
  • I tried linq query i posted in question but getting null because of FetchAttribute . How i can make it work ? – Neo Aug 27 '18 at 10:54
  • 1
    You could change `first` to`FirstOrDefault` to have a try. – Joey Cai Aug 27 '18 at 10:58
  • did the same but it come up null , i think we need to use FetchAttributes somewhere and then filter OrderByDescending(m => m.Properties.LastModified) i tried but fail. – Neo Aug 27 '18 at 11:01
  • we need to use ListFilesAndDirectories and FetchAttributes somehow – Neo Aug 27 '18 at 11:01