Question

every time i create a FileInfo object and access it's lastaccesstime property, it's always a few minutes off. the file property window remains constant, however the application shows that it is usually a few minutes after the property window time.

Also, i noticed that if i drag the file to the cmd window to pass the filename as an argument, the access time updates most of the time, but not always.

What could be causing this ?

below is an example:

static void Main(string[] args)
{
    if (args.Length > 0)
    {
        FileInfo fi = new FileInfo(args[0].ToString());
        Console.WriteLine(args[0]);
        if (fi.Exists)
        {
            Console.Write("Current: " + DateTime.Now + "\n");
            Console.Write("LAT: " + fi.LastAccessTime + "\n");
            Console.Write("LWT: " + fi.LastWriteTime + "\n");
            Console.Write("CT: " + fi.CreationTime + "\n");
        }
        Console.ReadKey();
    }
}

alt text http://img407.imageshack.us/img407/4728/propertiesox6.png alt text http://img380.imageshack.us/img380/7752/appgt0.png

Was it helpful?

Solution

In my experience, last access time is notoriously unreliable. According to http://technet.microsoft.com/en-us/library/cc781134.aspx...

The Last Access Time on disk is not always current because NTFS looks for a one-hour interval before forcing the Last Access Time updates to disk. NTFS also delays writing the Last Access Time to disk when users or programs perform read-only operations on a file or folder, such as listing the folder’s contents or reading (but not changing) a file in the folder.

Apparently, the in-memory copy will be correct, but in my experience, you may get a cached value which may be out of date. Also, note that last access time may be turned off by the user, and is turned off by default in Vista and 2008.

OTHER TIPS

The MSDN article with basic info about file times has this to say about file time resolution and Last Access times:

For example, on FAT, create time has a resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time has a resolution of 1 day (really, the access date). NTFS delays updates to the last access time for a file by up to one hour after the last access.

This would imply that on both FAT and NTFS, the Last Write Time will generally not be very precise, although I'm not sure the exact values they quote are correct.

Hmm, possibly this from MSDN:

When first called, FileSystemInfo calls Refresh and returns the cached information on APIs to get attributes and so on. On subsequent calls, you must call Refresh to get the latest copy of the information.

But you are seeing the LAT always being a few minutes in the [future|past]?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top