Нарушение общего доступа к файлам происходит после создания файла?
Итак, я пытаюсь создать файл .txt, а затем записать в него некоторые глупые данные. Но я получаю нарушение обмена. Я чувствую, что это может быть потому, что я пытаюсь создать StreamWriter для файла сразу после его создания, но это не имеет смысла. Так что я немного растерялся. Я попытался удалить все другие StreamWriters и Readers в классе, кроме ошибочной строки, и я все еще получаю нарушение. Ошибка, которую я получаю,
IOException: Sharing violation on path C:\Users\USER\Desktop\Accessible\Assets\IO\Books\A community of learners\frog\frog_status.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
Указывает на линию:
StreamWriter sw = new StreamWriter(statusTxtPath);
В следующей функции:
private IEnumerator GatherStatusInfo(string folderPath, string mediaName) {
string statusTxtPath = folderPath + @"/" + mediaName + "_status.txt";
_currentInfoBeingCreated._readingStatusTxtPath = statusTxtPath;
if(BuildManager.instance._isResetStatusFilesWhenStarting) {
if(File.Exists(statusTxtPath))
File.Delete(statusTxtPath);
}
if(!File.Exists(statusTxtPath)) {
File.Create(statusTxtPath);
StreamWriter sw = new StreamWriter(statusTxtPath);
sw.WriteLine(MediaStatus.Unread);
sw.WriteLine("0");
_currentInfoBeingCreated._status = MediaStatus.Unread;
_currentInfoBeingCreated._pageLastRead = 0;
sw.Flush();
sw.Close();
} else {
StreamReader statusReader = new StreamReader(statusTxtPath);
_currentInfoBeingCreated._status = (MediaStatus) Enum.Parse(typeof(MediaStatus), statusReader.ReadLine());
_currentInfoBeingCreated._pageLastRead = (int) ConversionUtils.instance.String2Float(statusReader.ReadLine());
statusReader.Close();
}
yield return 0;
}
Есть идеи, почему эта собака не будет охотиться?