IsolatedStorageFile giving exception
In windows phone 7, I am trying to write to a file and then trying to read from a file, but while reading it is giving the exception below.
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\\da开发者_如何学编程ta.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());
}
}
Exception:
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()
I am writing the file and then reading the file in the same method and not closing the emulator.
You need to call Close
on the StreamWriter
before writeToFile
returns (or better still wrap the code in a using
block).
The same applies to StreamReader
in readFromFile
.
精彩评论