IsolatedStorageFile给出异常

时间:2011-06-22 13:43:09

标签: windows windows-phone-7

在Windows Phone 7中,我正在尝试写入文件,然后尝试从文件中读取文件,但在阅读时会给出以下例外情况。

public static void writeToFile(String videoUrl)
{
    IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
    store.CreateDirectory("MyFolder");
    IsolatedStorageFileStream stream = new IsolatedStorageFileStream("MyFolder\\data.txt", FileMode.Append, 
                                            FileAccess.Write, store);
    StreamWriter writer = new StreamWriter(stream);
    writer.WriteLine(videoUrl);
}

public static void readFromFile()
{
    try
    {
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream stream = new IsolatedStorageFileStream("MyFolder\\data.txt", FileMode.Open,
                                                 store);
        StreamReader reader = new StreamReader(stream);
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            Debug.WriteLine("kkkkkk-----------------" + line); // Write to console.
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("ESPNGoalsUtil::readFromFile : " + ex.ToString());
    }

}

例外:

System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream.
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, IsolatedStorageFile isf)
   at ESPNGoals.Utility.ESPNGoalsUtil.readFromFile()

我正在编写文件,然后以相同的方法读取文件而不关闭模拟器。

1 个答案:

答案 0 :(得分:0)

您需要在StreamWriter返回之前调用writeToFile上的Close(或者更好地将代码包装在using块中)。

同样适用于StreamReader中的readFromFile