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 "";
}
}
}