IOException: Sharing violation on path. Please Help!

Hello, I was wondering how i keep getting this error and tried looking it up and got no results! I has hoping that someone could help me. This is the error:


IOException: Sharing violation on path C:\sdcard\Square\Highscores.data 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) Highscores+$WriteToFile$59+$.MoveNext () (at Assets/Scripts/Highscores.js:98)


and here is the Write To File code:


function WriteToFile(TextToWriteA: String) { var TextToWrite: String[] = TextToWriteA.Split(" " [0]); if (System.IO.File.Exists(FilePath + FileName)) {} else { System.IO.File.Create(FilePath + FileName); } yield WaitForSeconds(1); var sw: StreamWriter = new System.IO.StreamWriter(FilePath + FileName); for (var i = 0; i < TextToWrite.length; i++) { sw.WriteLine(TextToWrite*);* _StringToWrite += TextToWrite*;*_ _*}*_ _*sw.Close();*_ _*}_
_
----------*_
Thanks in Advance!

It looks like you still have a lock on the file you’re trying to write to. And I think it might have something to do with the File.Create() method you’re calling right before trying to access it with the StreamWriter.

I’m not familiar with JavaScript but in C# it’s possible to dispose the File.Create method right away like:

File.Create("filepath").Dispose();

Also, In C# it’s not necessary to create the file before using the StreamWriter so you can leave the File.Create() part out completely. I’m not sure if it will work in JS though.

You are createing the file in the update (every frame )