I have isolated the following code and it works inside the OnNavigatedTo event, so I know the code works. BUT, I can’t use it there. I need to use it inside the Suspending event. But it won’t work there. And when I set breakpoints, they’re not being hit anywhere inside this event. There are no compiletime or runtime errors either.
What is going on?
async void App_Suspending(
Object sender,
Windows.ApplicationModel.SuspendingEventArgs e)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}
I have found my solution. It involved checking whether or not the app was closed by the user. If so, it is (in my case, anyway) OK to delete these temporary files. You can do this in the App.xaml.cs file inside the OnLaunched method: