- Home /
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("\n" [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[i]); StringToWrite += TextToWrite[i]; } sw.Close(); }
Thanks in Advance!
Thanks but I did some more research and found out that streamwriter automatically creates the file if it does not exist.
Thanks for the answer anyways.
i I also had met the problem like yours, i found my read file is openning,and i colsed,it solved... wtf
Answer by Flightkick · Jun 21, 2015 at 06:09 PM
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.
for this answer. I dunno why unity didn't mention this. They did put an example of using File.Create, but that's it.
Thanks so much, this is exactly what I needed and I couldn't find it anywhere else on the net.
Answer by thekinghamza · Jul 12, 2018 at 01:55 AM
You are createing the file in the update (every frame )
Your answer
Follow this Question
Related Questions
How to create and write a text file after compile/build 0 Answers
Invalid File Error 2 Answers
HELP! Erorr! 3 Answers
2D array problem in C# 2 Answers
Access is denied in Windows 8 App 0 Answers