.NET:IsolatedStorageException

Nic*_*ner 5 .net c# isolatedstorage windows-phone-7

我正在Silverlight中构建一个Windows Phone 7应用程序.我遇到了困难IsolatedStorage.

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        if (!storage.FileExists(STORIES_FILE))
        {
            storage.CreateFile(STORIES_FILE);
        }

        string contents;

        // fails here
        using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Open))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                contents = reader.ReadToEnd();
            }
        }
Run Code Online (Sandbox Code Playgroud)

例外是:

"Operation not permitted on IsolatedStorageFileStream."
System.Exception {System.IO.IsolatedStorage.IsolatedStorageException}
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?MSDN表示删除或禁用隔离存储时会抛出此异常.这可能发生了吗?我在模拟器上遇到了这个问题.

更新:这似乎只在我第一次在模拟器上运行应用程序时发生.应用程序崩溃后,我再次在模拟器上运行它,并且不会发生此问题.

更新2:使用FileMode.OpenOrCreate而不是FileMode.Open似乎解决了问题.

Ste*_*ner 3

第一次运行应用程序时,该文件不存在,请尝试以下操作:

using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                contents = reader.ReadToEnd();
            }
        }
Run Code Online (Sandbox Code Playgroud)