我在Silverlight应用程序中使用IsolatedStorage进行缓存,因此我需要知道文件是否存在,我使用以下方法执行此操作。
我找不到用于 IsolatedStorage 的FileExists方法,所以我只是在捕获异常,但这似乎是一个非常普遍的异常,我担心它会比文件不存在时捕获更多。
有没有比这更好的方法来确定隔离存储中是否存在文件:
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 "";
}
}
}