Windows 8 winrt-System.UnauthorizedAccessException

Windows 8 winrt-System.UnauthorizedAccessException,windows-8,windows-runtime,Windows 8,Windows Runtime,我正在尝试写入和读取Win8的本地存储。使用帮助程序,我能够成功地向计算机写入,但读取.xml文件会导致:System.UnauthorizedAccessException发生在mscorlib.dll错误中 我创建了一个代码粘贴,以便您可以查看我的代码 代码在此失败: public async static Task<object> LoadData(string path, System.Type type) { var _Folder = Windows.Stora

我正在尝试写入和读取Win8的本地存储。使用帮助程序,我能够成功地向计算机写入,但读取.xml文件会导致:System.UnauthorizedAccessException发生在mscorlib.dll错误中

我创建了一个代码粘贴,以便您可以查看我的代码

代码在此失败:

public async static Task<object> LoadData(string path, System.Type type)
{
    var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;

    try
    {
        var _File = await Folder.GetFileAsync(path);

        using (IInputStream inStream = await _File.OpenSequentialReadAsync())

        {
            // Deserialize the Session State
            XmlSerializer x = new XmlSerializer(type);
            return x.Deserialize(inStream.AsStreamForRead());
        }
    }
    catch (Exception ex)
    {
        MessageDialog dialog = new MessageDialog(ex.Message.ToString());
        dialog.ShowAsync();            
        return null; 
    }
}
如果您对我的错误做法有任何看法,这将对我大有裨益。

我在发布预览版中这样做。如果我需要提供任何其他系统信息,请告诉我。

好的,以下是我注意到的。在文件
vehicleViewModel.cs
中调用
SaveList()
GetList()一个接一个。这两种方法都声明为
async void
,这意味着触发并忘记。我的想法是,当SaveList试图保存时,GetList试图同时读取一个文件。我试过你的代码,也犯了同样的错误。尝试将SaveList和GetList方法更改为
异步任务
,然后使用
等待

await SaveList();
await GetList();

这对我来说是个好办法。

使用调度程序,它会解决你的问题

wait CoreApplication.MainView.corewindown.Dispatcher.RunAsync(coredipatcherpriority.High,()=>
{
//你的UI更新代码在这里!
_dialogService.ShowMessage(rdm.ErrorMessage);
});


您忘记实际使用_Folder变量。1000谢谢!今晚我要试试这个。大家都说,这似乎是我需要的答案。我通常不会让Save和Get紧跟在一起,但是为了测试这个助手代码,我需要先获取数据,然后读取数据。今晚我试过之后,我会把这个标记为答案!再次感谢!
await SaveList();
await GetList();