Search code examples
uwpbackground-taskfile-watcher

UWP FileWatcher BackgroundTask


I'm new here and currently programming a Windows 10 Desktop UWP for my company, which should check a txt-file by a background task and update the UWP tile / UWP secondary tile.

Checking the users libraries by using the StorageLibraryContentChangedTrigger is simple and works fine. But my company says, that the users libraries is a not an good idea to save a txt-file (sample 1):

StorageLibrary videosLib = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);
StorageLibraryContentChangedTrigger videoTrigger = StorageLibraryContentChangedTrigger.Create(videosLib);

taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskNameFileChanged;
taskBuilder.TaskEntryPoint = taskFileChangedEntryPoint;
taskBuilder.SetTrigger(videoTrigger);
register = taskBuilder.Register();

Checking the app local folder by using this code (sample 2):

List<string> typeFilter = new List<string>();
typeFilter.Add(".txt");
var queryoptions = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderByName, typeFilter);
var query = ApplicationData.Current.LocalFolder.CreateFileQueryWithOptions(options);

 //subscribe on query's ContentsChanged event
 query.ContentsChanged += Query_ContentsChanged;


 private void Query_ContentsChanged(Windows.Storage.Search.IStorageQueryResultBase sender, object args)
 {
     Debug.WriteLine("File has changed!!!!");
 }

is also working, but only if the UWP is opened / active.

Q1: Is there a way, that triggers the ApplicationData.Current.LocalFolder, so i can say e.g. "ApplicationData.Current.LocalFolder.ContentChangedTrigger"?

Q2: If Q1 is not possible, how can i else check the ApplicationData.Current.LocalFolder for content changes by a background task?

Q3: I know that an UWP runs in a Sandbox, but can an UWP have a restricted access to a windows registry key? Read access would already suffice.

Thanks in advance for the answers :-)


Solution

  • Q1: Is there a way, that triggers the ApplicationData.Current.LocalFolder, so i can say e.g. "ApplicationData.Current.LocalFolder.ContentChangedTrigger"?

    No as far as I'm concerned. However, using the StorageLibraryContentChangedTrigger, you can put the text file in the Documents library, maybe it makes more sense for your company. I would tell your company that having an ideal UWP app is a dream at the moment.

    Q2: If Q1 is not possible, how can i else check the ApplicationData.Current.LocalFolder for content changes by a background task?

    You can register a Timer background task, then check the last modified date of the file every 15 minutes in background. Another alternative way is using push notifications to update the tile, instead of a local text file.

    Q3: I know that an UWP runs in a Sandbox, but can an UWP have a restricted access to a windows registry key? Read access would already suffice.

    No.

    Edit

    You need the Documents restricted capability to access the Documents folder. To make it work for development, manually add the entry to the xml Package.manifest file:

    <uap:Capability Name="documentsLibrary"/>
    

    In order to submit the app to the Store with this restricted capability, you need to request for the submission.

    Anyone may request access to this capability for store submission.