Search code examples
c#windows-phone-8serializationasync-awaitisolatedstoragefile

System.UnauthorizedAccessException: Access is denied. Windows Phone while using files


i'm getting a "System.UnauthorizedAccessException: Access is denied. " message when im trying to save a list on the Windows Phone.

Scenario:

When clicked on the FavoriteButton, the system will check if the item is already a favorite.

The next step is to call the Load method. This will return a list with favorites.

When the item is favorite I will remove it from the list, otherwise i will add it.

Final step is to save the new list with my Save method.

When executing this code i get the following exception:

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED

I've looked around and saw that it could be the manifest capabilities. So i have checked them all just to be sure. After trying again I still get the exception!

What must i do? code below:

     private async void favoriteClicked(object sender, EventArgs e)
    {
        if (_progressIndicator.IsVisible == false)
        {
            try
            {
                Boolean isFavorite = Settings.GetValueOrDefault<Boolean>(currentArtist.id, false);
                ArtistSaving favorite = new ArtistSaving(currentArtist.id, currentArtist.name, currentArtist.shortBio);
                artistList = await Task.Run(() => Utils.Load<ArtistSaving>());
                if (isFavorite == true)
                {
                    artistList.Remove(favorite);
                    displayDeletePopup();
                    Settings.AddOrUpdateValue(currentArtist.id, false);
                    await Task.Run(() => Utils.Save<ArtistSaving>(artistList));
                }
                else
                {
                    artistList.Add(favorite);
                    displayXamlPopup();
                    Settings.AddOrUpdateValue(currentArtist.id, true);
                     await Task.Run(() =>  Utils.Save<ArtistSaving>(artistList));
                }
            }
            catch (ArgumentException ex)
            {

            }
        }
    }

Favorite button click

public static async Task<ObservableCollection<T>> Load<T>()
    {
        // Create a new folder name DataFolder.
        var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(JSONFILENAME,
        CreationCollisionOption.OpenIfExists);

        var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));

        var myStream = await file.OpenStreamForReadAsync();

        ObservableCollection<T> myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);

        if (myObj == null)
        {
            return new ObservableCollection<T>();
        }
        return myObj;
    }

This is the Load method

 public static async void Save<T>(ObservableCollection<T> obj)
    {
        var serializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));

        using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
            JSONFILENAME, CreationCollisionOption.ReplaceExisting))
        {
            serializer.WriteObject(stream, obj);
        }
    }

and this was the Save method

EDIT -- SOLUTION

public static async Task<ObservableCollection<T>> Load<T>()
    {
        ObservableCollection<T> myObj = null;
        // Create a new folder name DataFolder.
        var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(JSONFILENAME,
        CreationCollisionOption.OpenIfExists);

        var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));

        using (var myStream = await file.OpenStreamForReadAsync())
        {
            myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);
            if (myObj == null)
            {
                return new ObservableCollection<T>();
            }
            return myObj;
        }


    }

Solution

  • Most likely the file is still locked from when you read from it. Modify your Load method like this to release the file lock:

    using (var myStream = await file.OpenStreamForReadAsync())
    {
        ObservableCollection<T> myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);
    }