5

我正在 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();
            }
        }

例外是:

"Operation not permitted on IsolatedStorageFileStream."
System.Exception {System.IO.IsolatedStorage.IsolatedStorageException}

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

更新:这似乎只发生在我第一次在模拟器上运行应用程序时。应用程序崩溃后,我再次在模拟器上运行它,并没有出现此问题。

更新 2:使用FileMode.OpenOrCreate代替FileMode.Open似乎已经解决了这个问题。

4

1 回答 1

3

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

using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                contents = reader.ReadToEnd();
            }
        }
于 2010-11-03T18:12:20.283 回答