So I've been working on a simple game and I wanted to implement a highscore system. Once the player loads up the main page for the first time a new text file is created ("hsc.txt") and some fake values are inserted which are later on split up by the program, however, currently my code throws a System.IO.IsolatedStorage.IsolatedStorageException
and I can't seem to find the problem. I've looked up the error that I got from the message box which was "- operation not permitted
" but all the solutions that were posted don't seem to work. I have tried closing the streams but it doesn't seem to work.
Any advice would be highly appreciated.
private void hasHighscores()
{
String fileName = "hsc.txt";
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoStorage.FileExists(fileName))
{
isoStorage.CreateFile(fileName);
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, FileAccess.Write, isoStorage))
{
using (var fileStream = new StreamWriter(isoStream))
{
fileStream.WriteLine("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888");
fileStream.Close();
}
isoStream.Close();
}
}
}
}
So far I have: a) changed the FileMode b) changed the FileAccess and a few other "quickfixes" that I don't even remember.
The
CreateFile
method returns a stream to the created file, and keeps it open. Therefore, when you try to open a stream to that same file in the next line, it throws an exception because the file is already locked.You can rewrite your code as follows:
I've also removed the
stream.Close()
instructions. The close method is automatically called when you enclose the stream in ausing
statement.