【问题标题】:IsolatedStorage in Window Phone 7.5Window Phone 7.5 中的独立存储
【发布时间】:2012-08-20 20:19:36
【问题描述】:

我正在使用 Windows Phone 7.5 中的独立存储。我正在尝试从文件中读取一些文本。但调试器说,IsolatedStorageFileStream 上不允许该操作。为什么?

//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("info.dat", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
string textFile = fileReader.ReadLine();

//Write the contents of the file to the MEssageBlock on the page.
MessageBox.Show(textFile);
fileReader.Close();

更新我的新代码

object _syncObject = new object();

                        lock (_syncObject)
                        {
                            using (var fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
                            {

                                using (FileStream stream = new FileStream("/info.dat", FileMode.Open, FileAccess.Read, FileShare.Read))
                                {
                                    using (var reader = new StreamReader(stream))
                                    {


                                        string textFile = reader.ReadLine();
                                        MessageBox.Show(textFile);

                                    }
                                }
                            }


                        }

                    }

【问题讨论】:

  • 请显示完整的堆栈跟踪。是在 IsolatedStorageFileStream 构造函数中,还是在您尝试读取一行时?
  • @jon-skeet 当我初始化'fileReader'时;

标签: c# windows-phone-7


【解决方案1】:

试试这个,它对我有用:希望它也对你有用

        String sb;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(fileName))
            {
                StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, myIsolatedStorage));

                sb = reader.ReadToEnd();

                reader.Close();
            }

            if(!String.IsNullOrEmpty(sb))
            {
                 MessageBox.Show(sb);
            }
        }

如果这不起作用,那么您的文件可能不存在。

【讨论】:

    【解决方案2】:

    通常当我使用隔离存储时,我会执行以下操作:

    using (var stream = fileStorage.OpenFile("info.dat", FileMode.Open))
    {
        using (var reader = new StreamReader(stream))
        {
            ...
        }
    }
    

    ... 而不是直接在IsolatedStorageFileStream 上调用构造函数。我不能确定这是否会解决问题,但值得一试......

    【讨论】:

    • 我更改了我的代码?但在这一行:使用 (var stream = fileStorage.OpenFile("info.dat", FileMode.Open)) debager 说 Operation not allowed on isolatedStorageFileStream.
    • 确定你已经成功地存储了文件吗?例如,您确定这不是指目录吗?你是如何初始化fileStorage的?
    • IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
    • @user1597524:您在创建文件时确定关闭了另一个流吗?这很奇怪……
    • 是的。当然。我使用 lock(_someobj)
    【解决方案3】:

    只是猜测:

    • WP 模拟器将在关闭时重置所有 Isolatd 存储内容
    • 如果您将 FileMode.Open 与指向不存在文件的路径一起使用,您将获得 Operation not allowed 异常。

    您可以使用fileStorage.FileExists() 来检查文件是否存在或使用FileMode.OpenOrCreate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多