Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying file changes in StorageFolder

I'm trying to keep a database in sync with the Windows 8 music library and I'm yet to find an efficient solution for doing so. I know that .NET has a FileSystemWatcher that's not available to Windows 8 apps. Currently, my idea is to compare the list of files returned by GetFilesAsync against my database and check if something was modified, deleted or added. I know this is not ideal but I can't find any other useful thing in Windows.Storage. My problem is that I want to make these updates automatically once there is a modification to the music library. Checking the ModifiedDate of the folders is useless when the changes happen in subfolders. Does anybody know if there is a way to tell when has a StorageFolder been modified?

like image 407
Chris Gutiérrez Avatar asked Jun 01 '26 08:06

Chris Gutiérrez


1 Answers

If you are able to get ContentsChanged to fire reliably, then the code below may help you determine what changed.

Note, it isn't fast. GetBasicPropertiesAsync appears to take ~5ms/file... so ~10 seconds to diff a set of 1000 files.

(I cannot get ContentsChanged to fire reliably, and, after hours of googling, it appears many others have the same problem)

    private class DiffSet
    {
        public IReadOnlyList<StorageFile> Added { get; set; }
        public IReadOnlyList<StorageFile> Deleted { get; set; }
        public IReadOnlyList<StorageFile> Changed { get; set; }
    }

    private static async Task<DiffSet> Diff(IEnumerable<StorageFile> oldSet, IEnumerable<StorageFile> newSet)
    {
        var newAsDict = newSet.ToDictionary(sf => sf.Path);

        var added = new List<StorageFile>();
        var deleted = new List<StorageFile>();
        var changed = new List<StorageFile>();

        var fromOldSet = new HashSet<string>();

        foreach (var oldFile in oldSet)
        {
            if (!newAsDict.ContainsKey(oldFile.Path))
            {
                deleted.Add(oldFile);
                continue;
            }

            var oldBasicProperties = await oldFile.GetBasicPropertiesAsync();
            var newBasicProperties = await newAsDict[oldFile.Path].GetBasicPropertiesAsync();

            var oldDateModified = oldBasicProperties.DateModified;
            var newDateModified = newBasicProperties.DateModified;

            if (oldDateModified != newDateModified)
            {
                changed.Add(oldFile);
            }

            fromOldSet.Add(oldFile.Path);
        }

        foreach (var newFile in newSet)
        {
            if (!fromOldSet.Contains(newFile.Path))
                added.Add(newFile);
        }

        return new DiffSet
        {
            Added = added,
            Deleted = deleted,
            Changed = changed
        };
    }
like image 56
Tristan Avatar answered Jun 04 '26 11:06

Tristan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!