Search code examples
.netisolatedstorage

How do I check if an IsolatedStorageFile exists?


We have code like:

try  
{   
   streamOptions = new IsolatedStorageFileStream(  “FileName”,  
                                                    FileMode.Open,  
                                                    FileAccess.Read);  
}  
catch ( FileNotFoundException )  
{  
   this.userSettings = new UserSettings();  
   load = false;  
}

This make Visual Studio break into the debugger often when I am debugging, therefore I wish to protect the above code with a “if”, so it only runs when the IsolatedStorageFile exists. However it is not clear how to use IsolatedStorageFile.FileExists() to check for the file that IsolatedStorageFileStream is about to open, e.g. what options do I have to give when I "new" a IsolatedStorageFile object.


Solution

  • private bool IsolatedStorageFileExists(string name)
    {
      using (var folder = IsolatedStorageFile.GetUserStoreForDomain())
      {
        return folder.FileExists(name);
      }
    }