When writing a txt file, it says sharing violation

Hi! I am making a game in Unity with c# as my main programming language. I try to make a new save.txt and autosave.txt if it doesn’t already exist in the app folder. It can create it, but it doesn’t work properly. Here are my codes:
Creating and writing a new save:

    void Start () {
	if(!File.Exists(Application.dataPath.ToString() + "/Save.txt"))
    {
        File.CreateText(Application.dataPath.ToString() + @"/Save.txt");
        saveFilePath = Application.dataPath.ToString() + @"/Save.txt";

        TextWriter writer = new StreamWriter(saveFilePath, false);
        writer.WriteLine("10:21:59", "13 / 06 / 2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500","2","1","1","5000", "10");
        writer.Close();
        
    }
    if(!File.Exists(Application.dataPath.ToString() + "/AutoSave.txt"))
    {
        File.CreateText(Application.dataPath.ToString() + @"/Autosave.txt");
        saveFilePath = Application.dataPath.ToString() + @"/Autosave.txt";

        TextWriter writer = new StreamWriter(saveFilePath, false);
        writer.WriteLine("00:00:00", "01 / 01 / 2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500", "2", "1", "1", "5000", "10");
        writer.Close();
    }
}

Here is my writing an existent .txt code:

    public void OnSaveGame()
{
    saveFilePath = Application.dataPath.ToString() + @"/Save.txt";
    isNewGame = false;

    TextWriter writer = new StreamWriter(saveFilePath, false);
    theTime = System.DateTime.Now.ToString("hh:mm:ss"); theDate = System.DateTime.Now.ToString("dd/MM/yyyy");
    string ZeroPart = theTime + "," + theDate + ",";
    string FirstPart = income;
    writer.WriteLine(ZeroPart + FirstPart);
    writer.Close();

    SavingPanel.SetActive(true);
    Invoke("WaitTime",2);
    writer.Close();        
    
}

I don’t know what I do wrong.
P.S. In unity,if it helps, when I run it, it says that it “IOException: Sharing violation on path C:*\Assets\Save.txt”

Application.dataPath is the path to the application data folder. This is the folder where the game is installed, and your game isn’t going to have permissions to write there. If you want a place to be able to write persistent data then use Application.persistentDataPath.

The sharing violation error is actually kind of a combination of “sharing violation, access denied, file in use”. In your case it’s most likely “access denied”.

You’ll need to dispose the writer after you’ve written it away. writer.Dispose();

This releases the file handles and allows it to be opened again.

A better way is to use the dispose/using pattern:

using(TextWriter writer = new StreamWriter(saveFilePath, false))
{
     theTime = System.DateTime.Now.ToString("hh:mm:ss"); theDate = System.DateTime.Now.ToString("dd/MM/yyyy");
     string ZeroPart = theTime + "," + theDate + ",";
     string FirstPart = income;
     writer.WriteLine(ZeroPart + FirstPart);
     writer.Close();
}

This will ensure the file is properly closed when you’re finished with it.

@cgarossi and @Dave-Carlile this is the modification:

    public void CreateFileTextSave()
    {
        if (!File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
        {
            File.CreateText(Application.persistentDataPath.ToString() + @"/Save.txt");
            saveFilePath = Application.persistentDataPath.ToString() + @"/Save.txt";
            using (TextWriter writer = new StreamWriter(saveFilePath, false))
            {
                writer.Write("00:00:00" + "01 / 01 / 2017" + "1" + "1" + "-21" + "20000" + "100" + "500" + "50" + "20" + "500" + "2" + "1" + "1" + "5000" + "10");
                writer.Close();
            }

        }

    }
    public void CreateFileTextAutoSave()
    {
        if (!File.Exists(Application.persistentDataPath.ToString() + @"/Autosave.txt"))
        {
            File.CreateText(Application.persistentDataPath.ToString() + @"/Autosave.txt");
            saveFilePath = Application.persistentDataPath.ToString() + @"/Autosave.txt";
            using (TextWriter writer = new StreamWriter(saveFilePath, false))
            { 
                writer.Write("00:00:00" + "01 / 01 / 2017" + "1" + "1" + "-21" + "20000" + "100" + "500" + "50" + "20" + "500" + "2" + "1" + "1" + "5000" + "10");
                writer.Close();
            }
        }
    }
    public void Start()
    {

        filePaths = new string[2] { @"/Autosave.txt", @"/Save.txt" };
        if (File.Exists(Application.persistentDataPath.ToString() + @"/Autosave.txt") && File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
        {
            //Code
        }
        if (!File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
            CreateFileTextSave();
        if (!File.Exists(Application.persistentDataPath.ToString() + @"Autosave.txt"))
            CreateFileTextAutoSave();

    }

but it still writes this error:


IOException: Sharing violation on path C:\Users\USER\AppData\LocalLow*\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)
MainMenuBasicBehaviour.CreateFileTextAutoSave () (at Assets/Scripts/MainMenuBasicBehaviour.cs:49)
MainMenuBasicBehaviour.Start () (at Assets/Scripts/MainMenuBasicBehaviour.cs:67)


Line 49 is using (TextWriter writer = new StreamWriter(saveFilePath, false))


Line 67 is CreateFileTextAutoSave();