所以,我正在尝试创建一个 .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;
}
知道为什么那条狗不会打猎吗?