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

1

8

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?

Rainger answered 28/9, 2016 at 2:36 Comment(0)
B
12

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.

Brutify answered 28/9, 2016 at 9:34 Comment(2)
Is only declared filetypes from manifest supported or any file types??Censure
@SoumyaMahunt I think you can try to use "*" to match all file types.Brutify

© 2022 - 2024 — McMap. All rights reserved.