Search code examples
c#androidvisual-studioxnamonogame

System.IO.IOException: Sharing violation on path


I receive the following sharing violation:

System.IO.IOException: 'Sharing violation on path /data/user/0/android_game.android_game/files/GameSave.txt'

When trying the following code using Visual Studio to write/read a file in the Environment.SpecialFolder.Personal directory

string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
FilePath = Path.Combine(FilePath, "GameSave.txt");

StreamWriter savefile = new StreamWriter(FilePath, true);
savefile.WriteLine("test");

StreamReader sr = new StreamReader(FilePath);
Console.WriteLine(sr.ReadLine());
sr.Close();

For context, I just require a basic text save file for a game, I'm not trying to share with another app. Any assistance would be welcome, thanks.


Solution

  • Figured it out after finding this thread: Sharing violation IOException while reading and writing to file C#

    The solution was to close the Streamwriter before reading the file using savefile.Close()