1

I need to inspect all files within a Repository periodically and compare its LastAccessTime in order to know when was the last time that file was used (accessed, modified and/or updated).

I've already been tried with File.GetLastAccessTime() and FileInfo.LastAccessTime, but I always get the same DateTime after open/close it, or read it.

How could I know when a file has been used?

Thanks!

string path = System.Environment.CurrentDirectory + "/testing/doc2.txt"
Console.WriteLine("--------------------------------------");
Console.WriteLine("GetLastAccessTime {0}", File.GetLastAccessTime(path));
Console.WriteLine("GetLastAccessTimeUtc {0}", File.GetLastAccessTimeUtc(path));
Console.WriteLine("Directory.GetLastAccessTime {0}", Directory.GetLastAccessTime(path));
FileInfo fi = new FileInfo(path);
Console.WriteLine("fi.LastAccessTime {0}", fi.LastAccessTime);

output:

GetLastAccessTime           12/18/2018 11:41:15 AM
GetLastAccessTimeUtc        12/18/2018 4:41:15 PM
Directory.GetLastAccessTime 12/18/2018 11:41:15 AM
fi.LastAccessTime           12/18/2018 11:41:15 AM
--------------------------------------
GetLastAccessTime           12/18/2018 11:41:15 AM
GetLastAccessTimeUtc        12/18/2018 4:41:15 PM
Directory.GetLastAccessTime 12/18/2018 11:41:15 AM
fi.LastAccessTime           12/18/2018 11:41:15 AM
Leonardo
  • 39
  • 1
  • 7
  • Are you saying the `File.GetLastAccessTime()` isn't reflecting when a file has been opened or read? – Martin Dec 18 '18 at 17:53
  • Read through the Remarks section of the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.getlastaccesstime?view=netframework-4.7.2) and make sure that your case doesn't fall into one of those conditions. – Sam W Dec 18 '18 at 17:56
  • 1
    From the documentation that @SamW mentions: _This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system._ – Martin Dec 18 '18 at 17:59
  • Can you add your code? Are you certain the file path is correct? As per [here](https://stackoverflow.com/a/2973642/3723088) you will still get a default `DateTime` value back if the files does not exist. What `DateTime` are you getting? – Sam W Dec 18 '18 at 18:18
  • @SamW you got it above – Leonardo Dec 18 '18 at 19:29
  • 1
    @Leonardo What DateTime are you expecting if 11:41:15 AM is not correct? Is your file system FAT, NTFS or something else? – Sam W Dec 19 '18 at 14:55
  • @SamW DateTime format is correct but always get the same value even though after open or read the file. – Leonardo Dec 19 '18 at 16:15
  • @Leonardo it seems you are right. I tested it on NTFS and FAT16 *(flash drive)*. Result: NTFS: time did not change even after the file was rewritten. last access time = creation time; test file was the test application. FAT16: last access time is correct day at 0:00:00 (midnight) in GMT+1 *(my time zone - CET)*, this means 23:00:00 previous day in UTC, and 1:00:00 for time saving period (summer time date). Tested in Windows 7. – Julo Dec 19 '18 at 17:27
  • I know the format is correct but what _time_ do you expect it to return? Again, what is your file system? – Sam W Dec 19 '18 at 17:28
  • @SamW NTFS and what I expect is that after open/read the file I get a different DateTime (a little more recent, of course). So, my idea is to compare day-by-day the DateTime of that file and, if it happens to be different, that would mean that the file was used (open or read or whatever) – Leonardo Dec 19 '18 at 19:05

1 Answers1

2

The most important thing is, what file system you are using. I suppose it is NTFS. Then there a few quotations:

Last access timestamp of a file is the last date and time when that file was opened for reading or writing. So every time a user access a file this timestamp needs to be updated, which is a bit of an overhead especially if you are not too keen on this file attribute. To improve the performance of NTFS filesystem in Windows 10 (and previous versions starting from Windows Vista) the last access times of files and directories are NOT updated.

Source here

Bonus chatter: Starting in Windows Vista, maintaining the last-access time is disabled by default. In practice, this means that the number of bugs related to altering the last-access time accidentally will multiply unchecked, because the mechanism for detecting the error is disabled by default.

Source here

And one more link, where it seems like there is still another behaviour.

And last link for FAT. I'm not so sure, since I last read only the FAT12 (floppy drive/diskettes) description many, many rears ago, but there probably is not a space to store last access time in FAT file systems, only the date is stored. I can not confirm this for the extension Microsoft provided with long file names.

Julo
  • 1,102
  • 1
  • 11
  • 19