O. Matei O. Matei - 4 months ago 58
C# Question

When writing a txt file in unity, it says sharing violation on path

I asked this question before, but I updated my code and the same problem rises. The script perfectly works when there are already the save files in the

pathToLoad(Application.persistentDataPath+[save name])
. Here is my code:

This is the saving/loading code:

public void GetDatesFromFile(string pathToLoad)
{
Debug.Log("It is here!");
if (File.Exists(pathToLoad))
{
Debug.Log("Exists");
using (StreamReader reader = File.OpenText(pathToLoad))
{
string currentLine = "";
string[] currentElements;
string[] separatorString = new string[] { "," };
currentLine = reader.ReadLine();
currentElements = currentLine.Split(separatorString, StringSplitOptions.RemoveEmptyEntries);
MainMenuBasicBehaviour.theTime = currentElements[0];
MainMenuBasicBehaviour.theDate = currentElements[1];
reader.Close();
}
}
else
{
Debug.Log("No Exists");
using (File.CreateText(pathToLoad))
{
using (TextWriter writer = new StreamWriter(pathToLoad, false))
{

writer.WriteLine("00:00:00,00/00/0000,1,1,500,20000,1500,50,20,10,5,2,1,1,0,10,50,50");
writer.Close();
}
MainMenuBasicBehaviour.theTime = "00:00:00";
MainMenuBasicBehaviour.theDate = "00 / 00 / 0000";
}
}
}


This is the main class MainMenuBasicBehaviour

public void GetDates()
{
for (int i = 0; i < filePaths.Length; i++)
{
filePath = Application.persistentDataPath.ToString() + filePaths[i];
Debug.Log(theDate + " " + theTime + filePath);
savingTextObject.GetDatesFromFile(filePath);
Debug.Log(theDate+" "+theTime+filePath);
SavingTexts[i].text = "Load game taken in:\n " + theDate + " at " + theTime;

}
}


And the error is the following:

IOException: Sharing violation on path C:\*\The Age Of Politics\Autosave.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
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)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path, Boolean append)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
SavingTextsScripts.GetDatesFromFile (System.String pathToLoad) (at Assets/Scripts/SavingTextsScripts.cs:168)
MainMenuBasicBehaviour.Start () (at Assets/Scripts/MainMenuBasicBehaviour.cs:33)


The lines it points to are:

SavingTextsScripts.cs:168 is the line: using (TextWriter writer = new StreamWriter(pathToLoad, false))
MainMenuBasicBehaviour.cs:33 is the line: savingTextObject.GetDatesFromFile(filePath);


I don't know what I did wrong. If more code is needed, I can provide it.

Answer Source

According to this Unity Answer post,

It looks like you still have a lock on the file you're trying to write to. And I think it might have something to do with the File.Create() method you're calling right before trying to access it with the StreamWriter.

Which matches this bit of your code and the error location:

using (File.CreateText(pathToLoad))
{
    using (TextWriter writer = new StreamWriter(pathToLoad, false))
    {

So you probably need to follow the same advice and call Dispose() before passing it to the streamwriter:

File.Create("filepath").Dispose();

Side note: using on that line is probably also not appropriate. You aren't actually declaring a variable to use, you're calling a method that returns an object that you're then forgetting about. Would probably be better to do this:

File.CreateText(pathToLoad)).Dispose();
using (TextWriter writer = new StreamWriter(pathToLoad, false))
{
    writer.WriteLine("00:00:00,00/00/0000,1,1,500,20000,1500,50,20,10,5,2,1,1,0,10,50,50");
    writer.Close();
}