3

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.

roguedev
  • 175
  • 1
  • 1
  • 12
  • You have to declare a file provider in order to give access to other apps . this link should help you https://forums.xamarin.com/discussion/151985/xamarin-fileprovider-cant-open-files-in-android – Jawad Zeb Jan 02 '20 at 10:53
  • Sorry but can you please be more specific? I'm not trying to give access to another app just read and write a text file in the same app. – roguedev Jan 02 '20 at 11:30
  • 1
    Does this answer your question? [Sharing violation IOException while reading and writing to file C#](https://stackoverflow.com/questions/11541244/sharing-violation-ioexception-while-reading-and-writing-to-file-c-sharp) – fdrobidoux Jan 06 '20 at 18:07

1 Answers1

3

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()

roguedev
  • 175
  • 1
  • 1
  • 12