Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 736
Daniel Armstrong Avatar asked Sep 28 '16 02:09

Daniel Armstrong


Video Answer


1 Answers

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.

like image 184
Grace Feng Avatar answered Oct 03 '22 09:10

Grace Feng