如何检查IsolatedStorageFile是否存在?

时间:2011-01-04 13:13:32

标签: .net isolatedstorage

我们的代码如下:

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

这使得Visual Studio在调试时经常会进入调试器,因此我希望使用“if”来保护上面的代码,因此它只在IsolatedStorageFile存在时运行。但是,目前尚不清楚如何使用IsolatedStorageFile.FileExists()来检查IsolatedStorageFileStream即将打开的文件,例如当我“新”一个IsolatedStorageFile对象时,我必须提供什么选项。

3 个答案:

答案 0 :(得分:3)

您可以在FileExists课程中使用IsolatedStorageFile方法。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(your_file_name)) { do something if file exist }
                    else { do something if file not exist}
            }