Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to save some informations into a file in the ApplicationData.Current.LocalFolder this way:

C#
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:

C#
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?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900