[SOLVED] "IOException: Sharing violation on path" Trying to save multiple files
What I'm trying to do is save 2 separate files as ".dat" file in different locations so that my code is more manageable.
My current serializable file has 2000+ lines of code, and that gets tedious to sift through, so I'm trying to create a second serializable file to split the saves, instead of adding another 1500 line to one file.
Script #1: GameControl (This one works)
public class GameControl : MonoBehaviour {
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
void OnEnable () {
Load();
}
void Start () {
InvokeRepeating("Save", 1.0f, 0.25f);
}
public void Save () {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
//hundreds of values
bf.Serialize(file, data);
file.Close();
}
public void Load () {
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
//hundreds of values
}
Script #1: MissionHanlder(This one messes everything up)
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class MissionHandler : MonoBehaviour {
void OnEnable () {
Load();
}
void Start () {
InvokeRepeating("Save", 1.3f, 0.23f);
}
public void Save () {
BinaryFormatter bf = new BinaryFormatter();
DirectoryInfo dirInf = new DirectoryInfo(Application.persistentDataPath + "/sm_save");
if(!dirInf.Exists){dirInf.Create();}
FileStream file = File.Create(Application.persistentDataPath + "/sm_save/sm_ParameterData.dat");
//a few dozen values
}
public void Load () {
if(File.Exists(Application.persistentDataPath + "/sm_save/sm_ParameterData.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/sm_save/sm_ParameterData.dat", FileMode.Open);
MissionData data = (MissionData)bf.Deserialize(file);
file.Close();
//a few dozen values
}
}
To Clarify, these are running concurrent with each other
Answer by archelyte_vz · May 17, 2017 at 02:22 PM
Turns out, this code segment is VERY important.
bf.Serialize(file, data);
file.Close();
Answer by dan_cmj · Feb 06, 2020 at 02:47 PM
For future references: I had this problem happen to me and none of these solutions I found online fixed it. Turns out the solution was to close Microsoft Excel which I was using to visualize the .csv
file I was modify with the game.
I hope this helps.
Answer by Bekatam · Jul 21, 2020 at 05:25 AM
What I did was declare my filestream on class level then gave the filestream different values in my methods, as in using it to read from a file in one and write to a file in another. Structure I used:
Class myClass
{
FileStream stream1;
void MethodToWrite()
{
stream1 = new FileStream(path, FileMode.Create);
using(var file = new StreamWriter(stream))
{
file.WriteLine("What I want to write");
}
}
void MethodToRead()
{
stream1 = new FileStream(path, FileMode.Open);
var file = new StreamReader(stream1);
string value = file.ReadLine();
}
}
This is not the most elegant way to solve this problem but it works.
Answer by Aqib_ch_dev · Mar 25, 2021 at 10:38 AM
New just add delay of 1 or 2 sec before saving this happend because you just create that file and trying to write the data .
SaveToDisk(Application.persistentDataPath + "/" + fileName + ".json", json);
public static IEnumerator SaveToDisk(string path,string data)
{
yield return new WaitForSeconds(1); File.WriteAllText(path, data);
}