Search Unity

IOException: Sharing violation on path

Discussion in 'Scripting' started by Zavalichi, May 10, 2019.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    I want to save some information into XML but I can't create the path...
    Code (CSharp):
    1. string path = Application.dataPath + "/" + SceneManager.GetActiveScene().name;
    2.         string XML = path + "/" + this.name;
    3.  
    4.         if (!Directory.Exists(path))
    5.             Directory.CreateDirectory(path);
    6.  
    7.         if (File.Exists(XML))
    8.             File.Delete(XML);
    9.         else
    10.             File.Create(XML);
    11.  
    12.  
    13.         XmlWriter xmlWriter = XmlWriter.Create(XML);
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What platform are you trying to do this on? Different platforms behave differently.

    Usually you don't want to use Application.dataPath. Instead use Application.persistantDataPath
     
  3. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Windows 10, Unity 2018.2.6f1
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the runtime value of XML? Debug.Log it!
     
  5. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    C:/Users/zzava/Desktop/Licenta/TrafficSimulator/Assets/TudorVladimirescu/Intersection2.xml

    Sometime it works, sometime it doesnt
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the difference, sometimes? Do you get an error?
     
  7. It's probably not the root cause of your problem (maybe, I haven't tried these things for ages), but this

    Code (CSharp):
    1.        if (File.Exists(XML))
    2.             File.Delete(XML);
    3.         else
    4.             File.Create(XML);
    5.  
    is inconsistent.
    Code (CSharp):
    1. if (File.Exists(XML))
    2.      File.Delete(XML);
    3. File.Create(XML);
    4.  
    Would be better.
     
  8. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Yes, when it doesnt work, i get error: IOException: Sharing violation on path C:\Users\zzava\Desktop\Licenta\TrafficSimulator\Assets\TudorVladimirescu\Intersection2.xml
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry, I would not want to guess at the actual error.
     
  10. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    IOException: Sharing violation on path C:\Users\zzava\Desktop\Licenta\TrafficSimulator\Assets\TudorVladimirescu\Intersection2.xml
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    On what line? Use Debug.Log to find out. It sounds like two processes or methods are trying to access the file at the same time.
     
  12. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Here: XmlWriter xmlWriter = XmlWriter.Create(XML);
     
  13. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Omg,
    I have XmlWriter xmlWriter = XmlWriter.Create(XML); and File.Create(XML); This is the problem. Thx bro