所以,我試圖創建一個.txt文件,然後寫一些愚蠢的數據。但是我收到了分享違規。我覺得這可能是因爲我試圖在創建文件後直接爲文件創建StreamWriter,但這沒有意義。所以我有點失落。我嘗試刪除課程中除錯誤行之外的所有其他StreamWriters和讀者,並且我仍然遇到違規行爲。我得到的錯誤,創建文件後發生文件共享衝突?
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;
}
任何想法,爲什麼狗不會打獵?
可能是File.Create將它保持打開狀態?我一直讓StreamWriter創建文件。 –