IsolatedStorageFile дает исключение - PullRequest
0 голосов
/ 22 июня 2011

В 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 голосов
/ 19 июня 2012

Вам нужно позвонить Close на StreamWriter до того, как writeToFile вернется (или, что еще лучше, обернуть код в блок using).

То же самое относится к StreamReader в readFromFile.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...