简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM