Search code examples
c#fileinfolastaccesstime

LastAccess Time is incorrect


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


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.