1

There is an impressive lack of examples of the usage of Refresh.

I'm using the following method, which gets an inaccurate time

 ViewBag.t1 = System.IO.File.GetLastAccessTime(@"C:\BillingExport\BILLING_TABLE_FILE01_1.txt");

I read that it's inaccurate because the OS hasn't performed a check and updated the files read/write times.

I've tried

 System.IO.FileSystemInfo.Refresh(@"C:\BillingExport\BILLING_TABLE_FILE01_1.txt");

But this does not work and I can't locate a resource giving similar examples of its usage.

Dave
  • 51
  • 1
  • 4
  • what happens if you assign the variable to something like this to check if you are truly getting a valid value back `var DateTime lastModified = System.IO.File.GetLastAccessTime(@"C:\BillingExport\BILLING_TABLE_FILE01_1.txt");` – MethodMan Aug 20 '14 at 21:21
  • 1
    possible duplicate of [.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong](http://stackoverflow.com/questions/1448716/net-fileinfo-lastwritetime-fileinfo-lastaccesstime-are-wrong) – MethodMan Aug 20 '14 at 21:26
  • And more info (not really a duplicate) in http://stackoverflow.com/questions/8312376/net-file-getlastaccesstime-updates-last-access-time-of-file – CodeCaster Aug 20 '14 at 21:38

2 Answers2

3

FileSystemInfo.Refresh is not a static method. What you have shown for your example does not compile. You should create a FileInfo object initialized with the file name and then you can call Refresh on that. You should then be able to use the properties of the FileInfo object to get the last access time and other pertinent file details.

var info = new FileInfo(@"C:\Temp\a.txt");
info.Refresh(@"C:\BillingExport\BILLING_TABLE_FILE01_1.txt");
var lastAccess = info.LastAccessTime;

One last edit based on an answer at the above linked possible duplicate and CodeCaster's answer:

http://blogs.technet.com/b/filecab/archive/2006/11/07/disabling-last-access-time-in-windows-vista-to-improve-ntfs-performance.aspx

Indicates that in Vista this was disabled by default. I just checked the registry in my Win 8.1 box and sure enough, the registry key is there and Last Access update is disabled by default. So, if you are on Vista or above the above code won't really work. If you are on XP than you should be golden!

Community
  • 1
  • 1
pstrjds
  • 16,840
  • 6
  • 52
  • 61
3

FileSystemInfo is the abstract base class for FileInfo and DirectoryInfo. Which cache the properties of a file/directory. If you keep, say, a FileInfo object around and keep testing its Exists property then it gets to be important that you call Refresh().

Which has nothing to do with File.GetLastAccessTime(). The classes are entirely unrelated, the File class does no caching and always retrieves the last access time from the file system.

Which is unreliable if the file is opened by any program. The file system is just not in a hurry to update these attributes when a program is actively accessing the file. That's way too expensive, that can easily cost many dozens of milliseconds to send the disk drive write head to the MFT sector that stores these values. A program can access a file much faster than that. Documented in this MSDN article:

Not all file systems can record creation and last access times, and not all file systems record them in the same manner. For example, the resolution of create time on FAT is 10 milliseconds, while write time has a resolution of 2 seconds and access time has a resolution of 1 day, so it is really the access date. The NTFS file system delays updates to the last access time for a file by up to 1 hour after the last access.

Most relevant phrase bolded, what you see is pretty much expected. You'll need to look for a different approach.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536