C# Windows Phone 7.5中的隔离存储

C# Windows Phone 7.5中的隔离存储,c#,windows-phone-7,C#,Windows Phone 7,我正在使用Windows Phone 7.5中的IsolatedStorage。我正试图从文件中读取一些文本。但调试器表示,不允许在IsolatedStorageFileStream上执行该操作。为什么? //Read the file from the specified location. fileReader = new StreamReader(new IsolatedStorageFileStream("info.dat", FileMode.Open, fileStorage));

我正在使用Windows Phone 7.5中的IsolatedStorage。我正试图从文件中读取一些文本。但调试器表示,不允许在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();
更新我的新代码

对象_syncObject=新对象()


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

using (var stream = fileStorage.OpenFile("info.dat", FileMode.Open))
{
    using (var reader = new StreamReader(stream))
    {
        ...
    }
}
。。。而不是直接在
IsolatedStorageFileStream
上调用构造函数。我不能肯定这是否会解决问题,但值得一试…

只是一个猜测:

  • WP emulator将在关闭时重置所有Isolatd存储内容
  • 如果您使用FileMode.Open,且路径指向不存在的文件,则会出现“操作不允许”异常

您可以使用
fileStorage.FileExists()
检查文件是否存在,或者使用
FileMode.open或create
试试这个,它对我有用:希望它也对您有用

        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);
            }
        }

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

请显示完整的堆栈跟踪。它是在IsolatedStorageFileStream构造函数中,还是当您尝试读取一行时?@jon skeet当我初始化“fileReader”时;我改代码了吗?但在这一行中:使用(var stream=fileStorage.OpenFile(“info.dat”,FileMode.Open))debager表示不允许对IsolatedStorageFileStream执行操作。您确定已正确存储该文件吗?例如,您确定它没有引用目录吗?如何初始化
文件存储
?IsolatedStorageFile文件存储=IsolatedStorageFile.GetUserStoreForApplication()@user1597524:创建文件时,您是否确实关闭了另一个流?这很奇怪……是的。当然,我用锁(someobj)
        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);
            }
        }