Search Unity

IOException: Sharing violation on path...

Discussion in 'Scripting' started by Josh1994, Apr 4, 2016.

  1. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    I getting an error when i'm trying to save to a .dat file, this error being 'IOException: Sharing violation on path (followed by the path)' here is my code for my Save and Load function.
    Code (csharp):
    1.  
    2. //Code now hidden
    3.  
    The line that is triggering the error is 'FileStream file = File.OpenWrite(Application.persistentDataPath + "/playerData.dat");'
    Also, I am an admin and do have access tot he folder I am trying to save to, so I am unsure why this error is coming up.
     
    Last edited: Apr 25, 2016
  2. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
    When I got this error before it was because I called the save/write function every frame. Make sure save is done and file is closed before trying to call it again.
     
  3. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    The issue i'm having the Save function can't run even once because of the error being triggered for line 6, however i've implemented a global bool with switches to fase after it runs once, however i've still got the same error
     
  4. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
    try

    Code (csharp):
    1.  
    2. string path = Application.persistentDataPath + Path.DirectorySeparatorChar + "playerData.dat";
    3.   BinaryFormatter bf = new BinaryFormatter();
    4.   FileStream file = File.Open(path, FileMode.OpenOrCreate);
    5.  
     
  5. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    I'm afraid I still get the same error, on the line 'FileStream file = File.Open(path, FileMode.OpenOrCreate);', if it helps here is the whole description of the error. (The bottom two lines are refering to function calls which lead to the Save function being called, the line above those is where the error is taking place)

    IOException: Sharing violation on path C:\Users\Josh\AppData\LocalLow\Leap Motion\Core Assets\playerData.dat
    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.File.Open (System.String path, FileMode mode) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:347)
    Level1.Save (Single totalDisplacementRoundedFloat, Single totalSDRoundedFloat, Single totalPercentageFloat, Leap.Hand hand) (at Assets/Scripts/Level1.cs:326)
    Level1.FinishedDrawing (Leap.Hand hand) (at Assets/Scripts/Level1.cs:182)
    Level1.Update () (at Assets/Scripts/Level1.cs:99)
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    An IOException can be thrown when the file is already open. Like vikankraft mentioned, this might mean you are opening it in two places in code. It might also mean you have it open in another editor. According to your call stack, you opening the file from Update() so there is at least a chance your flag is not working as intended and you are trying to open the file each frame. We'll need to see more of the code in order to help further.

    If you're certain the file is not open, inspect the Exception's HResult property and compare the value to the list of system error codes. IOException happens to be a "catch-all" for a number of error codes that will probably need to be treated in the same way.

    One comment about File.Open(). This method returns a FileStream object which needs to be cleaned up once you are done with it. Since FileStream implements the IDisposable interface, you can use the using keyword to make sure your object is cleaned up and the file is closed:

    Code (csharp):
    1. using (var file = File.Open(path, FileMode.OpenOrCreate))
    2. {
    3.   bf.Serialize(file, myObject);
    4. } // file is automatically closed after reaching the end of the using block
     
  7. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    Yep your right eisenpony turns out this reason it was failing was because I had the folder open in File Explorer, I kept deleting my playerData.dat file from there and left the folder open while I was debugging.
    Still can't believe that was what was affecting it, but there you go, thanks for all you guy's help!
     
    vikankraft likes this.
  8. KeithEvans

    KeithEvans

    Joined:
    Jun 18, 2015
    Posts:
    1
    Most of the time people save a game such as Read data > Update data > Write data. I solved my issue with this error by for a single time skipping directly to the Write step only once because somehow my file had become invalid for reading.
     
  9. oigetinthevan

    oigetinthevan

    Joined:
    Aug 1, 2018
    Posts:
    6
    Adding the using keyword solved this issue for me. Thanks for your help!
     
  10. RAGEeffect2926

    RAGEeffect2926

    Joined:
    Jan 3, 2021
    Posts:
    1
    not sure if an answer is still needed in this thread but all I did was delete the file and start the game twice (once to make the file and again to try reading) and it worked fine, I think my error was due to some overlap or something though and I'm not completely sure its the same problem
     
  11. radzradz

    radzradz

    Joined:
    Jan 5, 2021
    Posts:
    6
    restarting Unity worked for me
     
    Filip8429 likes this.
  12. Aqib_ch_dev

    Aqib_ch_dev

    Joined:
    Dec 22, 2019
    Posts:
    2
    just add delay of 1 or 2 sec before saving
    SaveToDisk(Application.persistentDataPath + "/" + fileName + ".json", json);

    public static IEnumerator SaveToDisk(string path,string data)
    {
    yield return new WaitForSeconds(1);
    File.WriteAllText(path, data);

    }
     
  13. dastopher

    dastopher

    Joined:
    Jun 27, 2013
    Posts:
    1
    Pausing Dropbox sync fixed this issue for me
     
  14. omony

    omony

    Joined:
    Apr 2, 2020
    Posts:
    6
    Have constant issues with Kaspersky System Watcher - blocks Unity internal operations as well with the same exception.
     
    Fitbie likes this.
  15. icaroleles1

    icaroleles1

    Joined:
    Feb 18, 2018
    Posts:
    6
    this 100% worked for me
     
  16. Fitbie

    Fitbie

    Joined:
    Aug 17, 2021
    Posts:
    66
    Turning off antivirus (Kaspersky in my case) solves the issue. Seems like it does some skanning during Unity initializing
     
  17. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Hello, I am from future :D
    Code (CSharp):
    1.  
    2.   path = Application.persistentDataPath + "/ save.file";
    3.   BinaryFormatter bf = new BinaryFormatter();
    4.   FileStream file = File.Open(path, FileMode.Open);
    5.   // Bal Bla string value = file.binary;
    6.   // And then:
    7.   file.Close();
    8.  
    And
    File saver also need to be closed before saving. Like this:
    Code (CSharp):
    1.  
    2.             FileStream file = File.Create(Application.persistentDataPath + "save.file");
    3.             formatter.Serialize(file, value);
    4.             file.Close();
    5.  
    If file isn't closed on one of the functions, it is more likely is to have access violation.
    I have some codes that works for saving and loading by using only and only two functions. I don't like to share things like that anymore. But at least there you go. I would be happy to know if works with your situation! :):):)
    Half Reveal:
    upload_2023-6-27_14-32-19.png
     
    ActiveSim likes this.
  18. yashubhati

    yashubhati

    Joined:
    Mar 28, 2017
    Posts:
    8
    You can try deleting the file on the path. If the file exist on the path and you try to deserialise the file, after modifying to your code this might throw errors.

    Let know if this works.
    :D
     
    Last edited: Jul 14, 2023