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:
private void hasHighscores()
{
String fileName = "hsc.txt";
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoStorage.FileExists(fileName))
{
using (var isoStream = isoStorage.CreateFile(fileName))
{
using (var fileStream = new StreamWriter(isoStream))
{
fileStream.WriteLine("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888");
}
}
}
}
}
I've also removed the stream.Close()
instructions. The close method is automatically called when you enclose the stream in a using
statement.