Why does File Explorer read files when I just browse a directory?
When you're browsing a directory in File Explorer, you may notice that files in this directory are opened and read. This can have negative effects on performance if your data storage is remote. This article reviews the possible causes and workarounds for such behavior.
In general, files are opened before any operation including the reading of their attributes can be carried out on them. Your application should not immediately react on every opening and start fetching remote file data. The least an OpenFile event handler should do is inspect the value of the DesiredAccess parameter. This parameter roughly indicates, for which purpose a process is opening a file or directory. In many cases, the value will be just FILE_READ_ATTRIBUTES (value 0x0080) or, rarely, FILE_READ_EA (value 0x0008) or their combination. When the DesiredAccess parameter is set this way, your application does not need to do anything special besides possibly checking that the file still exists.
File Explorer and the Shell in general (whose UI includes system Open and Save dialogs) may attempt to generate thumbnails for, and obtain other metadata about, the files in the directory you are browsing. This often requires a portion of a file to be read rather than the whole file, but even so, the ReadFile event will fire and your application must handle it. If such behavior is not desirable, there are some strategies you can employ to help mitigate it:
- Include the STGMP_NETWORK flag when calling the AddMountingPoint method; this will create a "network mounting point", and File Explorer does its best to minimize the number requests it makes against such mounting points by default.
- When handling the EnumerateDirectory and GetFileInfo events, add the FILE_ATTRIBUTE_RECALL_ON_OPEN (value 0x00040000) and FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS (value 0x00400000) to your file attributes. These relatively new attributes, added in Windows 10 version 1607, tell processes not to read file data unless really necessary.
- In earlier versions of Windows, your application can report that the files in your virtual filesystem have the FILE_ATTRIBUTE_OFFLINE attribute. File Explorer won't attempt to generate thumbnails for offline files. However, keep in mind that File Explorer will visibly mark (i.e., display an overlay on the icon of) any file with this attribute, so this solution may not be desirable for that reason.
- Proactively cache the information that File Explorer will request so that you can quickly handle the ReadFile events during directory enumeration.
- For some types of drives/mounting points, File Explorer will create a thumbs.db file within the drive and use it to cache thumbnail data. Your application can attempt to pre-create this file, or store it somewhere it can be accessed quickly.
An antivirus or similar software may attempt to read file data on every (or first) file open regardless of the value of the DesiredAccess parameter of the open request. That software is required to check and respect the FILE_ATTRIBUTE_RECALL_ON_OPEN and FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS flags mentioned above. If it doesn't do this, the only option is to complain to the corresponding software vendor.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@callback.com.