How can I save data to virtual folder?


CBFS Shell implements a shell namespace extension—a mechanism that enables the creation of virtual folders within the Windows Shell namespace. Virtual folders allow you to present custom data as items, which can appear as directories or files. These items might correspond to actual files and folders on real filesystems, or they might be entirely virtual, sourcing their data from a variety of locations. In CBFS Shell terminology, the former are called “physical” items, while the latter are “virtual” items.

Although virtual folders are part of the Windows Shell, they are not part of any actual filesystem. As a result, standard operating system mechanisms that applications use to read and write files do not function directly with virtual folders.

When you invoke the Open command on a non-folder shell item (i.e., an item in a virtual folder), one of several scenarios can occur: * Physical item: If the file is backed by a real file on a filesystem, the Shell uses that file as the data source. It simply passes the file path to the application opening the item. * Virtual item: If the item is virtual, the Shell requests its data from the namespace extension. In this case, CBFS Shell fires the GetData event. The Shell then saves the received data to a temporary file on a real filesystem, and passes the path of this temporary file to the application.

Let’s consider how applications work with files. When a file is opened for reading, the application is given a file path to read from. If the application needs to save changes, it typically uses one of two approaches: * Overwrite: Open the original file and overwrite its contents with new data. * Replace: Write the new data to a separate file, then rename it to replace the original file (minimizing the risk of data loss during the save operation).

Most modern applications use the second approach for safety.

In both scenarios, writing occurs on the filesystem, not in the virtual shell folder. As mentioned earlier, a shell folder is not a filesystem, whereas applications rely on Windows APIs designed to interact with actual filesystems.

This means that, regardless of how data is written, the process bypasses the virtual shell folder. If the virtual folder represents a real directory, your shell extension can monitor changes in the underlying directory and update the virtual folder's contents accordingly. However, if writing is done to a file in a temporary directory (as in the case of virtual items), the new data does not make its way back to the virtual folder, and your shell extension will have no knowledge of these changes.

Advanced developers might discover that the Shell supports writable virtual shell items. While this is technically possible—meaning applications could, in theory, write directly to virtual shell items—most modern applications do not utilize this mechanism, rendering it largely unused.

If your application needs to present remote data that can be updated (such as a remote Document Management System), you should implement a virtual filesystem, which can be done using our CBFS Connect product. This virtual filesystem can be exposed either as a drive letter or mapped to a directory, then presented as a virtual shell folder using CBFS Shell. In this setup, items in the virtual shell folder can be made “physical,” representing files in the virtual filesystem. This makes it possible for applications to write data, thereby updating the contents of the virtual folder.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@callback.com.