gpt4 book ai didi

c# - 有没有比这更好的方法来确定 IsolatedStorage 文件是否存在?

转载 作者:行者123 更新时间:2023-11-30 15:48:24 24 4
gpt4 key购买 nike

我在 Silverlight 应用程序中使用 IsolatedStorage 进行缓存,因此我需要知道该文件是否存在,我使用以下方法执行此操作。

我找不到 IsolatedStorage 的 FileExists 方法,所以我只是捕获异常,但这似乎是一个非常一般的异常,我很担心与文件不存在相比,它会捕获更多信息。

有没有比这更好的方法来查明文件是否存在于 IsolatedStorage 中:

public static string LoadTextFromIsolatedStorageFile(string fileName)
{
string text = String.Empty;

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName,
FileMode.Open, isf))
{
using (StreamReader sr = new StreamReader(isfs))
{
string lineOfData = String.Empty;
while ((lineOfData = sr.ReadLine()) != null)
text += lineOfData;
}
}
return text;
}
catch (IsolatedStorageException ex)
{
return "";
}
}
}

最佳答案

来自“手册”(.net framework 2.0 应用程序开发基础):

不同于针对任意存储文件的应用程序编程接口(interface)(API)在文件系统中,独立存储中文件的API不支持检查直接像 File.Exists 一样获取文件的存在。相反,您需要询问存储匹配特定文件掩码的文件列表。如果找到,您可以打开文件,如本例所示

string[] files = userStore.GetFileNames("UserSettings.set");
if (files.Length == 0)
{
Console.WriteLine("File not found");
}
else
{
// ...

}

关于c# - 有没有比这更好的方法来确定 IsolatedStorage 文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2636827/

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