c# - .NET:独立存储异常

标签 c# .net windows-phone-7 isolatedstorage

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

异常(exception)情况是:

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

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

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

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

最佳答案

第一次运行应用程序时,该文件不存在,请试试这个:

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

关于c# - .NET:独立存储异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4090123/

相关文章:

c# - 读取远程计算机上的内存量时出现问题

c# - 访问 EF 导航属性时避免 NullReferenceException

c# - 如何使用 TrueType 自定义嵌入字体在 WebBrowser 控件中显示内容?

xaml - 如何将 xaml Storyboard绑定(bind)到 WP7 中自定义控件的 Point 属性?

internet-explorer - 防止在 Windows Phone 7 的 IE 中滚动

c# - 算法改进

c# - .NET Framework SDK 如何与可用的目标框架相关联?

c# - IAsyncCursor 如何与 mongodb c# 驱动程序一起用于迭代?

c# - 在 docker 容器外公开日志文件

c# - COM 组件在控制台应用程序中可以 100% 工作,但从服务调用时会失败