2

我试图弄清楚如何读取和写入存储在应用程序隔离存储中的参数。

现在我正在构建一个 windows phone 应用程序,但是由于我想要一个 win8 应用程序,所以我想我可以在可移植类库项目中做到这一点,并发现了这个很棒的 PCLStorage

我的 Cache 类看起来像这样用于存储参数:

    public async static Task<string> GetParam(string name)
    {
        IFolder rootfolder = FileSystem.Current.LocalStorage;
        IFolder folder = await rootfolder.GetFolderAsync("isostore");
        IFile file = await folder.GetFileAsync(name);

        return await file.ReadAllTextAsync();
    }

    public async static void SaveParam(string name, string param)
    {
        IFolder rootfolder = FileSystem.Current.LocalStorage;
        IFolder folder = await rootfolder.CreateFolderAsync("isostore", CreationCollisionOption.OpenIfExists);
        IFile file = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);

        await file.WriteAllTextAsync(param);
    }

写入部分是好的,如果有的话,它会覆盖。它的阅读部分是问题所在。IFile 和 IFolder 没有任何.Exists功能 (???) 那么如果我在保存之前调用 Get 会返回什么?

4

1 回答 1

1

I think in your case in the GetParam method you should be calling CreateFolderAsync and CreateFileAsync with the CreationCollisionOption.OpenIfExists parameter. Then you shouldn't need to worry about creating them separately beforehand or catching exceptions that they do/don't exist.

于 2013-10-03T21:03:49.967 回答