UWP 内容更改事件

UWP Event on content changed

我想观察一个文件夹。就像我想要一个事件当内容改变时。 我找到了这个

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
 }

但问题是我找不到哪个文件导致了事件,我什至不知道是什么导致了事件(比如修改、创建、删除或重命名文件)如何获取这些详细信息?

我使用了这个代码

    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;
 }

这段代码不能正常工作我看起来是因为 addtoOld 是异步的代码不能被替换,因为它是递归的。并且功能无法同步它有很多等待我该如何解决这个问题?

注意:OLD 和 New 是列表 ChangeType 和 Type 是枚举

根据以下博客post,遗憾的是无法确定事件的原因,也没有关于受影响项目的信息。

WinRT 中的文件系统更改通知: http://lunarfrog.com/blog/filesystem-change-notifications

所以我想您将必须浏览所有文件并检查它们的 Properties 属性 以确定每个文件的最后修改时间、创建时间等:https://docs.microsoft.com/en-us/uwp/api/windows.storage.storagefile