Search code examples
c#uwpasync-awaitstoragefolder

UWP Event on content changed


I want to observe on a folder. Like i want a Event When the content is changed. I found this

var options = new Windows.Storage.Search.QueryOptions
{
          FolderDepth = Windows.Storage.Search.FolderDepth.Deep
};
var query = Folder.CreateFileQueryWithOptions(options);
query.ContentsChanged += QueryContentsChanged;
var files = await query.GetFilesAsync();

 private  void QueryContentsChanged(IStorageQueryResultBase sender, object args)
 {
        //Code here
 }

But the problem with this is I can't find which file caused the event and i even can't know what caused the Event (Like Modify , Create , Delete or Rename of file) How to get these details?

I used this code

    public async void MonitorFolder()
    {
        var options = new Windows.Storage.Search.QueryOptions
        {
            FolderDepth = Windows.Storage.Search.FolderDepth.Deep
        };

        var query = Folder1.CreateFileQueryWithOptions(options);
        query.ContentsChanged += QueryContentsChanged;
        var files = await query.GetFilesAsync();
        await addtoOld(Folder1, Old);
    }
 private async void addtoOld(StorageFolder folder1, List<FDate> old)
    {
        var files = await folder1.GetFilesAsync();
        foreach (var file in files)
        {
            BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
            FDate f = new FDate
            {
                Path = file.Path,
                Id = file.FolderRelativeId,
                Modified = basicProperties.DateModified,
                Change = ChangeType.NoChange,
                FileType = Type.File
            };
            old.Add(f);
        }
        var folders = await folder1.GetFoldersAsync();
        foreach (var folder in folders)
        {
            BasicProperties basicProperties = await folder.GetBasicPropertiesAsync();
            FDate f = new FDate
            {
                Path = folder.Path,
                Id = folder.FolderRelativeId,
                Modified = basicProperties.DateModified,
                Change = ChangeType.NoChange,
                FileType = Type.Folder
            };
            old.Add(f);
            addtoOld(folder, old);
        }
        return;
    }

private async void QueryContentsChanged(IStorageQueryResultBase sender, object args)
    {
        New.Clear();
        List<FDate> changed = new List<FDate>();

        await addtoOld(Folder1, New);
        foreach(var f in New)
        {
            var f1 = getFile(f);
            if (f1 != null)
            {
                if (f1.Modified < f.Modified)
                {
                    f1.Change = ChangeType.Modified;
                    changed.Add(f1);
                }
                Old.Remove(f1);
            }
            else
            {
                f.Change = ChangeType.Created;
                changed.Add(f);
            }
        }
        foreach (var f in Old)
        {
            f.Change = ChangeType.Deleted;
            changed.Add(f);
        }
        Old = New;
 foreach (var f in changed)
 {
     if(f.FileType== Type.File)
     {
          if (f.Change == ChangeType.Modified)
          {
               //code here
          }
           if(f.Change == ChangeType.Created)
           {
                //Created code here
           }
           if(f.Change == ChangeType.Deleted)
           {
               //Deleted code here
           }
      }
      else
      {
           if (f.Change == ChangeType.Created)
           {
                   //Created code here     
           }
           if(f.Change == ChangeType.Deleted)
           {
                 //Deleted code here           
           }
      }
}
private FDate getFile(FDate f)
{
    foreach(var fi in Old)
    {
        if (f.Name == fi.Name)
               return fi;
    }
    return null;
 }

This code is not working properly I looks like it is because the addtoOld is async The code can't be substituted because it is recursive. and the function can't be made sync it has many await how do i solve this?

Note:OLD and New are Lists ChangeType and Type are enums


Solution

  • According to the following blog post, there is unfortunately no way to identify the reason of the event and there is also no information about affected items.

    File system change notifications in WinRT: http://lunarfrog.com/blog/filesystem-change-notifications

    So I guess you will have to go through all files and check their Properties property to determine when each file was last modified, created etc.: https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefile