IOException: Sharing violation on path

I want to save some information into XML but I can’t create the path…

string path = Application.dataPath + "/" + SceneManager.GetActiveScene().name;
        string XML = path + "/" + this.name;

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        if (File.Exists(XML))
            File.Delete(XML);
        else
            File.Create(XML);


        XmlWriter xmlWriter = XmlWriter.Create(XML);

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

Windows 10, Unity 2018.2.6f1

What is the runtime value of XML? Debug.Log it!

C:/Users/zzava/Desktop/Licenta/TrafficSimulator/Assets/TudorVladimirescu/Intersection2.xml

Sometime it works, sometime it doesnt

What is the difference, sometimes? Do you get an error?

It’s probably not the root cause of your problem (maybe, I haven’t tried these things for ages), but this

       if (File.Exists(XML))
            File.Delete(XML);
        else
            File.Create(XML);

is inconsistent.

if (File.Exists(XML))
     File.Delete(XML);
File.Create(XML);

Would be better.

Yes, when it doesnt work, i get error: IOException: Sharing violation on path C:\Users\zzava\Desktop\Licenta\TrafficSimulator\Assets\TudorVladimirescu\Intersection2.xml

Sorry, I would not want to guess at the actual error.

IOException: Sharing violation on path C:\Users\zzava\Desktop\Licenta\TrafficSimulator\Assets\TudorVladimirescu\Intersection2.xml

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.

Here: XmlWriter xmlWriter = XmlWriter.Create(XML);

Omg,
I have XmlWriter xmlWriter = XmlWriter.Create(XML); and File.Create(XML); This is the problem. Thx bro