So, I'm attempting to create a .txt file, and then write some silly data to it. But I'm getting a sharing violation. I sense that it may be because I'm attempting to create a StreamWriter for a file directly after creating it, but that doesn't make sense. So I'm a bit lost. I've tried removing all the other StreamWriters and Readers in the class except for the erroneous line, and I'm still getting a violation. The error I'm getting,
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)
Points to the line:
StreamWriter sw = new StreamWriter(statusTxtPath);
In the following function:
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;
}
Any idea why that dog won't hunt?
Is there a reason why you are wanting to create the file then reopen it to write to it. StreamWriter has an method that will do just that. It will create a new file if it doesn't exist.
From above link:
Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.