1

I want to monitor folder for recursive changes. I use this code:

public async void StartMonitor(string folderPath)
{
    StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(folderPath);
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.FolderDepth = FolderDepth.Deep;
    StorageItemQueryResult query = storageFolder.CreateItemQueryWithOptions(queryOptions);
    query.ContentsChanged += QueryContentsChanged;
    await query.GetItemsAsync();
}

private void QueryContentsChanged(IStorageQueryResultBase sender, object args)
{
    //to do
}

Event is fired once after StatMonitor executes. But then when I create file or folder in this directory, event is not fired. How can I get this event firing?

N. Saw
  • 103
  • 2
  • 7

2 Answers2

2

The query object was going out of scope. I made the query object a private class field and it works.

N. Saw
  • 103
  • 2
  • 7
0

Based on the document - StorageFolderQueryResult.ContentsChanged Event, it mentions that you need to call the StorageFileQueryResult.GetFilesAsync Method for at least once to make the event work. So please try to call the GetFilesAsync method first before you handle the ContentsChanged event.

Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
  • Thank you, Roy. I found my error. The query object was going out of scope. I made the query object a private class field and it works. – N. Saw Mar 09 '22 at 13:55
  • @N.Saw Glad to hear that. If you have found your issue. Please post it here as answer and mark it. – Roy Li - MSFT Mar 10 '22 at 01:09