I'm trying to save some informations into a file in the ApplicationData.Current.LocalFolder this way:
private async Task WriteRecentScores(List<ScoreRecord> list)
{
try
{
var serializer = new DataContractJsonSerializer(typeof(List<ScoreRecord>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
RECENTSCOREFILE,
CreationCollisionOption.ReplaceExisting))
{
serializer.WriteObject(stream, list);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}
}
but I get an Unauthorized Access exception when reaching OpenStreamForWriteAsync.
RECENTSCOREFILE="score.json" is a file created when launching the app for the very first time (I'm using the FailIfExists option), here's the code:
private async void CreateFile()
{
try
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(RECENTSCOREFILE, CreationCollisionOption.FailIfExists);
}
catch (Exception) { }
return;
}
The funny thing is that I tried to launch the app both in the emulator and in my phone and it seems that it throws the exception only when running on the phone.
I figured out that it works only the first time I write the file, but how can I solve this?