How can I watch for file changes in a UWP project?

耗尽温柔 提交于 2019-12-05 07:10:22
Grace Feng

Is there an equivalent for UWP?

You can subscribe the ContentChanged event for the queried storage files.

For example:

List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".txt");
fileTypeFilter.Add(".png");
var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderByName, fileTypeFilter);
var query = ApplicationData.Current.LocalFolder.CreateFileQueryWithOptions(options);
//subscribe on query's ContentsChanged event
query.ContentsChanged += Query_ContentsChanged;
var files = await query.GetFilesAsync();

private void Query_ContentsChanged(Windows.Storage.Search.IStorageQueryResultBase sender, object args)
{
    //TODO:
}

This searches and returns all .txt and .png files in app's local folder, if the files in the query are added to, deleted from, or modified, this event will be fired.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!