Which feature of a file system is responsible for keeping track of changes in the system?
Skip to main content This browser is no longer supported. Show Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Track file system changes in the background
In this articleImportant APIs
The StorageLibraryChangeTracker class allows apps to track changes in files and folders as users move them around the system. Using the StorageLibraryChangeTracker class, an app can track:
Use this guide to learn the programing model for working with the change tracker, view some sample code, and understand the different types of file operations that are tracked by StorageLibraryChangeTracker. StorageLibraryChangeTracker works for user libraries, or for any folder on the local machine. This includes secondary drives or removable drives but does not include NAS drives or network drives. Using the change trackerThe change tracker is implemented on system as a circular buffer storing the last N file system operations. Apps are able to read the changes off of the buffer and then process them into their own experiences. Once the app is finished with the changes it marks the changes as processed and will never see them again. To use the change tracker on a folder, follow these steps:
The next sections walk through each of the steps with some code examples. The complete code sample is provided at the end of the article. Enable the change trackerThe first thing that the app needs to do is to tell the system that it is interested in change tracking a given library. It does this by calling the Enable method on the change tracker for the library of interest.
A few important notes:
Wait for changesAfter the change tracker is initialized, it will begin to record all of the operations that occur within a library, even while the app isn’t running. Apps can register to be activated any time there is a change by registering for the StorageLibraryChangedTrigger event. Read the changesThe app can then poll for changes from the change tracker and receive a list of the changes since the last time it checked. The code below shows how to get a list of changes from the change tracker.
The app is then responsible for processing the changes into its own experience or database as needed. Tip The second call to enable is to defend against a race condition if the user adds another folder to the library while your app is reading changes. Without the extra call to Enable the code will fail with ecSearchFolderScopeViolation (0x80070490) if the user is changing the folders in their library Accept the changesAfter the app is done processing the changes, it should tell the system to never show those changes again by calling the AcceptChangesAsync method.
The app will now only receive new changes when reading the change tracker in the future.
Important things to rememberWhen using the change tracker, there are a few things that you should keep in mind to make sure that everything is working correctly. Buffer overrunsAlthough we try to reserve enough space in the change tracker to hold all the operations happening on the system until your app can read them, it is very easy to imagine a scenario where the app doesn’t read the changes before the circular buffer overwrites itself. Especially if the user is restoring data from a backup or syncing a large collection of pictures from their camera phone. In this case, ReadBatchAsync will return the error code StorageLibraryChangeType.ChangeTrackingLost. If your app receives this error code, it means a couple things:
It should be rare to get these cases, but in scenarios where the user is moving a large number of files around on their disk we don’t want the change tracker to balloon and take up too much storage. This should allow apps to react to massive file system operations while not damaging the customer experience in Windows. Changes to a StorageLibraryThe StorageLibrary class exists as a virtual group of root folders that contain other folders. To reconcile this with a file system change tracker, we made the following choices:
Calling the Enable methodApps should call Enable as soon as they start tracking the file system and before every enumeration of the changes. This will ensure that all changes will be captured by the change tracker. Putting it togetherHere is all the code that is used to register for the changes from the video library and start pulling the changes from the change tracker.
FeedbackSubmit and view feedback for |