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

ぃ、小莉子 提交于 2019-12-07 03:38:48

问题


I am porting my game to UWP from the full desktop .net and one thing I need to work out is how to live load texture, shaders etc... into the UWP version of the game.

In the desktop version I use a FileSystemWatcher to do this but FileSystemWatcher doesn't exist in UWP, even on directory's that I have full control over.

Is there an equivalent for UWP?

What is the best way to implement this with the limited set of API's in UWP?


回答1:


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.



来源:https://stackoverflow.com/questions/39737147/how-can-i-watch-for-file-changes-in-a-uwp-project

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