System.UnauthorizedAccessException:拒绝访问。使用文件时的Windows Phone

时间:2015-02-24 17:23:04

标签: c# windows-phone-8 serialization async-await isolatedstoragefile

我正在尝试在Windows Phone上保存列表时收到“System.UnauthorizedAccessException:Access is denied。”消息。

场景:

当点击FavoriteButton时,系统将检查该项目是否已成为收藏夹。

下一步是调用Load方法。这将返回一个包含收藏夹的列表。

当项目是收藏​​夹时,我会将其从列表中删除,否则我将添加它。

最后一步是使用我的Save方法保存新列表。

执行此代码时,我得到以下异常:

  

System.UnauthorizedAccessException:访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED

我环顾四周,看到它可能是明显的能力。所以我已经检查了所有这些只是为了确定。再试一次后,我仍然得到了例外!

我该怎么办?代码如下:

     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)
            {

            }
        }
    }

收藏按钮单击

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;
    }

这是加载方法

 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);
        }
    }

这是Save方法

编辑 - 解决方案

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;
        }


    }

1 个答案:

答案 0 :(得分:4)

当您从中读取文件时,该文件很可能仍处于锁定状态。像这样修改Load方法以释放文件锁:

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