c# - 为什么我不能在 WinRT 中从 AppData 读取 XML?

标签 c# xml windows-8 windows-runtime microsoft-metro

我想要的
从 AppData.Local 中获取一个 xml 文件,并将其序列化为一个列表

我编码什么
错误部分:

List<myTask> AllTaskList = await  objectStorageHelper.LoadAsync();

myTask 是一个简单的类:

public class myTask
{
    public string myTitle { get; set; }
    public string myDuetime { get; set; }
}

objectStorageHelper 是来自 CodePlex 的 HelpClass , LoadAsync 部分如下:

    public async Task<T> LoadAsync()
    {
        try
        {
            StorageFile file = null;
            StorageFolder folder = GetFolder(storageType);
            file = await folder.GetFileAsync(FileName());
            //file = await folder.CreateFileAsync("BetterTask.xml", CreationCollisionOption.OpenIfExists);
            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
            Stream inStream = Task.Run(() => readStream.AsStreamForRead()).Result;
            return (T)serializer.Deserialize(inStream);
        }
        catch (FileNotFoundException)
        {
            //file not existing is perfectly valid so simply return the default 
            return default(T);
            //Interesting thread here: How to detect if a file exists (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb)
            //throw;
        }
        catch (Exception)
        {
            //Unable to load contents of file
            throw;
        }
    }

错误是什么

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access Denied。 (Exception from HRESULT:0x80070005 (E_ACCESSDENIED))

If there is a handler for this exception, the program may be safely continued.

--
为什么会这样?
我可以使用这个帮助类成功写入文件。
但是为什么我没有读取文件的权限?
如何解决?

最佳答案

改变

StorageFolder folder = GetFolder(storageType); 

StorageFolder folder = ApplicationData.Current.LocalFolder

如果这可行,则问题出在您的文件夹权限上。

关于c# - 为什么我不能在 WinRT 中从 AppData 读取 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063592/

相关文章:

c# - 重命名具有相同用途的多个参数的好方法是什么?

c# - 使用 C# 停止 QTP 执行

java - 根据 firebase 远程配置更改 res 文件夹中的 xml 文件

Java Xml 解码类型转换

c# - 如何将输入事件路由到共享同一区域的多个 Canvas 中的特定 Canvas ?

c# - 用于呈现图像的 ASPX 页面突然无法在 Firefox 中运行。适用于 Chrome/IE

c# - 为什么是 "Using asynchronous [...] methods on CPU-bound [providing] no benefits and results in more overhead."

javascript - 在我打开开发者工具之前,IE 不加载 XML 内容?

c# - 如何在 WinRT 中同时允许多个弹出窗口?

c# - 如何将本地存储图像复制到剪贴板 WinRT