内容上的 UWP 事件已更改



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

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 是异步的 代码无法替换,因为它是递归的。 并且该功能无法同步 它有很多等待 我该如何解决这个问题?

注意:旧和新是列表更改类型和类型是枚举

根据以下博客文章,不幸的是,没有办法确定事件的原因,也没有关于受影响项目的信息。

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

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

相关内容

  • 没有找到相关文章

最新更新