Problem with error: IOException: Sharing violation on path

I'm having the following error in the unity console:

IOException: Sharing violation on path /Users/carlos/_Personal/Development/Unity3d/B2BUnity1/Assets/Documents/Profiles.xml System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) 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.Xml.XmlTextWriter..ctor (System.String filename, System.Text.Encoding encoding) ProfileManager.StoreProfiles (System.Collections.ArrayList profilesList) (at Assets/Scripts/ProfileManager.cs:86) GUITouchScroll.OnGUI () (at Assets/Scripts/GUITouchScroll.cs:113)

And the code at ProfileManager.cs:86

public void StoreProfiles(ArrayList profilesList) {

         // Create a new file specified path
       XmlTextWriter textWriter = new XmlTextWriter(profilesPath, null); // 86 
        // Opens the document
        textWriter.WriteStartDocument();

Code at GUITouchScroll.cs:113

ProfileManager profileMan = plane.GetComponent("ProfileManager") as ProfileManager;
    profileMan.profilesPath = Application.dataPath + "/Documents/Profiles.xml";
profileMan.StoreProfiles(list);  //113  

Is it possible that using Applicaction.dataPath is causing the problem? or maybe is it something related to XmlTextWriter?

I encountered the “sharing violation” exception when running the editor in OS X but NOT when running in Windows 7.
The error is ambiguous but it would appear that a stream is not being closed resulting in a conflict when you attempt to create a new reader/writer.

The solution is as follows:

XmlWriterSettings settings = new XmlWriterSettings();
settings.CloseOutput = true;
using(XmlWriter writer = XmlWriter.Create(file.CreateText(), settings)){	
  //do work here
}

The XmlWriterSettings.CloseOutput will guarantee that the stream is closed when your XmlStream is disposed. Hopefully that will solve your problems :slight_smile:

source: stackoverflow

I got the same Problem, Solution:

close all Windows and programs that use your opened unity project, especially close Visual Studio, then it works wihtout the error message

Anyone solved this problem?

Thanks

I ran into this error recently and found that it had nothing to do with my actual file IO. It turned out that I had duplicate objects. As you could imagine, two objects both trying to open and write to a file ends in bad things. So as a troubleshooting step you may want to double check what objects you instantiate and which ones were added through the editor (created at startup). Be sure to remove objects from the editor that you plan to instantiate later.