0

I have a basic c# UWP app running on Windows 10.

I'm trying to get the last access time for a StorageFile like this:

var basicProperties = await file.GetBasicPropertiesAsync();
var moreProperties = await basicProperties.RetrievePropertiesAsync(
    new string[] { "System.DateAccessed" });
var dateAccessed = moreProperties["System.DateAccessed"];

However, the date I get is wrong. It is too far in the past.

If I take a look the Properties in the Windows explorer or on the command line, the date is correct and updates each time I open the file in my app.

Get-ChildItem '.\test.txt' | select -ExpandProperty lastacc

Only in my app, the last access time is wrong. Am I trying to access the wrong property or is there another way to get the last access time?

UPDATE:

It turns out that File.GetLastAccessTime(file.Path); does return the correct time as it is reported on the command line. I went for the StorageFile route, because the app is sandboxed and did not try the path based API before.

All of the above assumes that the file system is set up to keep track of the last access date in the first place.

Mark
  • 6,647
  • 1
  • 45
  • 88
  • I believe the last access time is updated only, when you close the handle to the file. As long as you keep the file handle open, the file meta data isn't updated. – IInspectable Jul 20 '16 at 14:24
  • I do close the file handle. I can't reproduce this each time, but I have the feeling that sometimes the value I get is the "second last" last modified date. – Mark Jul 21 '16 at 06:16
  • Failure to reproduce an issue is usually caused by non-deterministic garbage collection. Closing a file handle is asynchronous: the managed wrapper is put on the finalizer queue, and even when the native `CloseHandle` call returns, it takes time for the OS to determine, that the last handle to a file object has been close, and to flush data to the device. For testing, you can add explicit `GC.Collect()` and `Thread.Sleep()` calls, and see, if the issue remains. – IInspectable Jul 21 '16 at 08:49

0 Answers0