Alireza Peer
Alireza Peer

Reputation: 908

IOException: Sharing violation on path

here is my code to create a .dat file or append text to it:

public static void saveGameAnalyseData(GameAnalyseData gameAnalyseData)
{
    Debug.Log(JsonUtility.ToJson(gameAnalyseData) + " " + Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat");

    if (File.Exists(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat"))
    {
        File.AppendAllText(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat", JsonUtility.ToJson(gameAnalyseData) + Environment.NewLine);
    }
    else
    {
       File.Create(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat");
        if (File.Exists(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat"))
        {
            File.AppendAllText(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat", JsonUtility.ToJson(gameAnalyseData) + Environment.NewLine);
        }
    }
}

But i get

IOException: Sharing violation on path on the second File.AppendAllText call...

i also tried this code:

    if (File.Exists(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat"))
    {
        File.AppendAllText(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat", JsonUtility.ToJson(gameAnalyseData) + Environment.NewLine);
    }
    else
    {
       File.Create(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat");
        StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat");
        sw.WriteLine(JsonUtility.ToJson(gameAnalyseData) + Environment.NewLine);
        // close the stream
        sw.Close();
    }

but i get same error on this line:

StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat");

PS: I should mention that when the file exists, it append the text to it correctly, the problem is when creating the file and writing to it for the first time

Upvotes: 0

Views: 2943

Answers (2)

Viktor Surzhko
Viktor Surzhko

Reputation: 21

In my case, the problem was Anti-Virus, with intensive writing to a file, Anti-Virus began to scan the file and at that moment there was a problem with writing to the file.

Upvotes: 1

user6755993
user6755993

Reputation:

You can just replace all of your code with just this line of code:

File.AppendAllText(Application.persistentDataPath + "/" + gameAnalyseData.gameID + ".dat", JsonUtility.ToJson(gameAnalyseData) + Environment.NewLine);

Because the AppendAllText method opens a file, appends the specified string to the file, and then closes the file, if the file doesn't exist, this method creates a file, writes the specified string to the file then closes the file.

Anyway if you want to use your own code, everywhere that you use File.Create(filePath); Replace with this code, File.Create(filename).Close(); . Because File.Create(filePath) creates or overwrite a file in a specified path and then return an open FileStream object to that file. If you want, you can use this FileStream object to work with your file, if you don't want so you should close it.

Upvotes: 1

Related Questions