IOException: Sharing violation on path when trying to write to file.

I’m trying to follow tutorials here so I’m unsure what I’m doing wrong.

Here’s what I believe is causing the problem. These two are disrupting each other. I’m trying to write the data to the files in the second sample.

DefaultDataMap = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).ToString() + "/Bot";

I’ve confirmed this works.

saveDataPath = DefaultDataMap + "/Bot/" + msg.userName.ToString() + ".dat";

        if (!File.Exists(saveDataPath))
            Debug.Log("File non-existant...");
            File.Create(Application.persistentDataPath + "/Bot/" + msg.userName.ToString() + ".dat");
            Debug.Log("ID Non-existant! Creating...");
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/Bot/" + msg.userName.ToString() + ".dat", FileMode.Open);

            PlayerDataTS data = new PlayerDataTS();
   = NewPlayer;
   = DefaultCash;

            bf.Serialize(file, data);

No .dat file is created. msg.username works.

a sharing violation usually means there is allready another program that is using the same file when you are trying to write to it. i would guess a simple conflict between your build and your editor. try making sure the you only have one copy of your code running.

Answered my own question.

saveDataPath = DefaultDataMap +"/"+ msg.userName.ToString() + ".txt";

        if (!File.Exists(saveDataPath))
            Debug.Log("File non-existant...");
            StreamWriter writer = new StreamWriter(saveDataPath);
            writer.WriteLine(msg.userName + "" + DefaultCash.ToString()); //Creates editable data in form of a string.
        } else
            Debug.Log("It Exists...");