Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching FileInfo properties in C#

From the MSDN documentation for the FileInfo.Name property, I see that the data for the property is cached the first time it is called and will only be updated subsequently by using the Refresh method.

I've the following questions which I can't find or aren't too clear in the documentation:

  1. Is the data for all properties cached at the same time?

  2. Is the Refresh method called on creation of the FileInfo, or only when a property is called for the first time?

  3. If I've called one property, e.g. the Name property, and it's called Refresh, will calling a different property, e.g. the DirectoryName property, for the first time cause it to call Refresh again, or is it only called by the first property accessed in the entire class (see question #1)?

  4. Can I pre-cache all the properties by calling Refresh manually? (Assuming it's not pre-cached on construction of the object)

  5. Does calling Refresh manually cause properties which are pre-cached, e.g. CreationTime, to be refreshed as well?

like image 295
Iain Sproat Avatar asked Jun 13 '11 17:06

Iain Sproat


2 Answers

  1. At a guess, yes. It seems like a bit of a self-defeating "optimisation" for FileInfo to fetch only the properties you've fetched before, especially when they can be (and probably are) all fetched in one API call.

  2. The fact that the documentation calls out DirectoryInfo methods which serve up already-cached FileInfos suggests quite strongly (to me, anyway) that simply constructing a FileInfo doesn't cache anything. It makes sense - if you construct a FileInfo directly, it might refer to a file that doesn't exist yet (you plan to create it, for instance), whereas all the methods which return cached FileInfos refer to files that exist at the time of the snapshot, under the assumption you're going to use at least some of them.

  3. No, by my answer to question 1. That's why the Refresh method is there.

  4. I would imagine so (see answer 1).

  5. Yes. See answer 3.

like image 74
anton.burger Avatar answered Nov 20 '22 16:11

anton.burger


The value of the CreationTime property is pre-cached if the current instance of the FileSystemInfo object was returned from any of the following DirectoryInfo methods:

  • GetDirectories
  • GetFiles
  • GetFileSystemInfos
  • EnumerateDirectories
  • EnumerateFiles
  • EnumerateFileSystemInfos

To get the latest value, call the Refresh method.

If the file described in the FileSystemInfo object does not exist, this property will return 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

NTFS-formatted drives may cache file meta-info, such as file creation time, for a short period of time. This process is known as file tunneling. As a result, it may be necessary to explicitly set the creation time of a file if you are overwriting or replacing an existing file.

(MSDN)

Internally, Refresh calls the standard Win32API and thus fills all properties.

[...]
flag2 = Win32Native.GetFileAttributesEx(path, 0, ref data);

Accessing any property that is specified to Refresh causes a full refresh, for instance:

public DateTime LastAccessTimeUtc
{
    [SecuritySafeCritical]
    get
    {
        if (this._dataInitialised == -1)
        {
            this._data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
            this.Refresh();
        }
        [...]
like image 30
mafu Avatar answered Nov 20 '22 14:11

mafu