This code works great for creating the file if it does not exist.
if ( !File.Exists(level11path) )
File.CreateText(level11path);
And this is a function I have that fills an empty file with “default” info.
fileIO.ResetFile(level11path);
Alone, both of them work great. However when I try this.
if ( !File.Exists(level11path) )
File.CreateText(level11path);
fileIO.ResetFile(level11path);
Instead of creating the file and then filling it with the default data it throws the following exception when I try to open the the stream writing with this code
var sw : StreamWriter = new StreamWriter(savePath);
The exception reads:
IOException: Sharing violation on path /Users/…mypath/
My suspicion is that it’s trying to open the write path to a file that doesn’t actually exist yet or that some sort of permission is incorrect? Any thoughts on what I’m doing wrong?
where do you write it to?
Documents folder of the user?
/Documents/ for the app path, the path is working well, I can create the file fine and then later modify it without trouble, or if the file is already there I can modify it and save data from the game whether its on the iPhone or in the editor. The only problems comes when I try and create it and then immediately write to it.
So it seems that after calling CreateText it is not releasing it’s handle on the file, that is why I’m getting the exception, but I don’t really see any documentation explaining how to release it like you can do with the StreamWriter.Close().
So in case it helps anybody else here is what I’m doing.
I don’t think the CreateText function is necessary anyway. When you create a new streamwriter it has the option to check if the file exists and if not it will create it, the MSDN docs say you need to include a flag but this seems to be on by default so the flag would be to disable the check. So in this case I can just run my “write to file” function and if there is no file there it will create it, write to it, and then I can close the connection with Close().
This is working fine so far on iPhone and in the Editor, anybody have a reason why this is a poor approach?