- Home /
System.IO sharing error despite closing the read and write
system.io is giving a sharing file error when using these two functions (not simultaneously) here are the two functions that handle saving and loading from what i can tell it looks to be something to do with loading because it can load fine but when trying to save after a load it will give a sharing error but i haven't tested it the other way around, i know the code is long but i wanted to include both functions because the error code (from what i can see) doesn't tell much info about what line caused the error but rather the error within System.IO itself there are a few lines in the error that are associated with the saving function though but i think its referring to what it was calling rather than the saving function leaving its stream Writer open and not closing it
function saveGameList ()
{
var sw : StreamWriter = new System.IO.StreamWriter(Application.dataPath + "/" + "games.list"); //opened to same directory
for(var i = 0; i < names.length; i++)
{
sw.WriteLine(fitToFileFormat(i)); //loops through slots and combines info in 7 different arrays to 1 variable to look like "hello-1-1-1-1-1-1"
}
sw.WriteLine("#"); //signals the array info is over
sw.WriteLine(core.money); //writes player variables
sw.WriteLine(core.compName); //writes player variables
sw.WriteLine(core.playerName); //writes player variables
sw.Close(); //both read and write are being closed
}
function loadGameList ()
{
var sr : StreamReader = new System.IO.StreamReader(Application.dataPath + "/" + "games.list"); //opened to same directory
var found = false;
for(var i = 0; i < 15 && found == false; i++)
{
var line = sr.ReadLine();
Debug.Log(line);
if(line == "#") //checks to see if the array info is over then sets the player variables after the for loop
{
found = true;
}
else
{
var game = line.Split("-"[0]);
addNewGame(game[0],parseInt(game[1]),parseInt(game[2]),parseInt(game[3]),parseInt(game[4]),parseInt(game[5]),parseInt(game[6])); //adds the info to the 7 arrays described at the top
}
}
core.money = parseInt(sr.ReadLine()); //sets player variables
core.compName = sr.ReadLine(); //sets player variables
core.playerName = sr.ReadLine(); //sets player variables
sr.Close(); //both read and write are being closed
}
here is the full error that unity gives
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/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-runtime-and-classlibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string)
CoreMechanic.saveGameList () (at Assets/CoreMechanic.js:108)
CoreMechanic.addNewGame (System.String name, Int32 platform, Int32 genre, Int32 style, Int32 client, Int32 initialCost, Int32 weeklyCost) (at Assets/CoreMechanic.js:43)
gameCreateMaster.createGame () (at Assets/gameCreateMaster.js:26)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
ive tried alot of techniques to help with it like .flush() and .Dispose() but its still gives the sharing error