【发布时间】:2010-04-14 19:20:15
【问题描述】:
我在 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 "";
}
}
}
【问题讨论】:
标签: c# silverlight isolatedstorage