Is there a better way than this to find out if an IsolatedStorage file exists or not?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-04-14T11:15:42Z Indexed on 2010/04/14 11:23 UTC
Read the original article Hit count: 509

Filed under:
|
|

I'm using IsolatedStorage in a Silverlight application for caching, so I need to know if the file exists or not which I do with the following method.

I couldn't find a FileExists method for IsolatedStorage so I'm just catching the exception, but it seems to be a quite general exception, I'm concerned it will catch more than if the file doesn't exist.

Is there a better way to find out if a file exists in IsolatedStorage than this:

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

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about isolatedstorage