gpt4 book ai didi

c# - 在隔离存储中缓存变量,可移植类库方式

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:23 24 4
gpt4 key购买 nike

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

现在我正在构建一个 Windows Phone 应用程序,但由于我想要一个 win8 应用程序,我想我可以在可移植类库项目中完成它,并发现这个很棒 PCLStorage .

我的缓存类看起来像这样存储参数:

    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 会返回什么?

最佳答案

我认为在您的情况下,在 GetParam 方法中,您应该使用 CreationCollisionOption.OpenIfExists 参数调用 CreateFolderAsync 和 CreateFileAsync。那么您不必担心事先单独创建它们或捕获它们存在/不存在的异常。

关于c# - 在隔离存储中缓存变量,可移植类库方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19152316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com