Anodyne
Anodyne

Reputation: 1852

Avoid updating last-accessed date/time when reading a file

We're building a Windows-based application that traverses a directory structure recursively, looking for files that meet certain criteria and then doing some processing on them. In order to decide whether or not to process a particular file, we have to open that file and read some of its contents.

This approach seems great in principle, but some customers testing an early version of the application have reported that it's changing the last-accessed time of large numbers of their files (not surprisingly, as it is in fact accessing the files). This is a problem for these customers because they have archive policies based on the last-accessed times of files (e.g. they archive files that have not been accessed in the past 12 months). Because our application is scheduled to run more frequently than the archive "window", we're effectively preventing any of these files from ever being archived.

We tried adding some code to save each file's last-accessed time before reading it, then write it back afterwards (hideous, I know) but that caused problems for another customer who was doing incremental backups based on a file system transaction log. Our explicit setting of the last-accessed time on files was causing those files to be included in every incremental backup, even though they hadn't actually changed.

So here's the question: is there any way whatsoever in a Windows environment that we can read a file without the last-accessed time being updated?

Thanks in advance!

EDIT: Despite the "ntfs" tag, we actually can't rely on the filesystem being NTFS. Many of our customers run our application over a network, so it could be just about anything on the other end.

Upvotes: 5

Views: 11843

Answers (4)

Luke
Luke

Reputation: 11431

The documentation indicates you can do this, though I've never tried it myself.

To preserve the existing last access time for a file even after accessing a file, call SetFileTime immediately after opening the file handle with this parameter's FILETIME structure members initialized to 0xFFFFFFFF.

Upvotes: 6

Yordan Voyvode
Yordan Voyvode

Reputation: 53

Also you can use the method File.GetLastAccessTime(fileInfoLocation); before the code that will change the LastAccessTime and then after the code thats going to change the LastAccessTime you can use the method File.SetLastAccessTime(fileInfoLocation, originalLastAccessTime); to set it back to what it was though you need Administrative privileges for the SetLastAccessTime part if you want to change it to the original AKA run as admin. Heres some code for example:

FileInfo fileInfoLocation = new FileInfo(getAllExeLocation[l]);
                                                DateTime originalLastAccessTime = File.GetLastAccessTime(fileInfoLocation.FullName);//Save the LastAccessTime before it was changed by Icon.Extract Method
                                                exeIcon = Icon.ExtractAssociatedIcon(fileInfoLocation.FullName);//This changes LastAccessTime
                                                File.SetLastAccessTime(fileInfoLocation.FullName, originalLastAccessTime);//Set the LastAccessTime to the originalOne though it needs Privileges to do this one(run as admin) or Use Try-Catch statement

Upvotes: -1

Suresh
Suresh

Reputation: 183

From Vista onwards NTFS does not update the last access time by default. To enable this see http://technet.microsoft.com/en-us/library/cc959914.aspx

Starting NTFS transaction and rolling back is very bad, and the performance will be terrible.

You can also do

FSUTIL behavior set disablelastaccess 0

Upvotes: 4

Jim
Jim

Reputation: 2146

I don't know what your client minimum requirements are, but have you tried NTFS Transactions? On the desktop the first OS to support it was Vista and on the server it was Windows Server 2008. But, it may be worth a look at.

Start an NTFS transaction, read your file, rollback the transaction. Simple! :-). I actually don't know if it will rollback the Last Access Date though. You will have to test it for yourself.

Here is a link to a MSDN Magazine article on NTFS transactions which includes other links. http://msdn.microsoft.com/en-us/magazine/cc163388.aspx

Hope it helps.

Upvotes: 1

Related Questions