- Home /
How Do I Write to a Created File?
Hello, I'm working on a saving and loading system. In the system, you can create as many saves as you want, creating a new file for each save. First, I create the file using:
File.Create(filePath);
The path is defined using:
worldsFolder = Application.dataPath + "/WorldFolder";
string filePath = worldsFolder + "/" + worldName + ".world";
I created a string array with each line as a data set and tried to write to the file using:
File.WriteAllLines(filePath, fileLines);
But when I click my save button in-game, an error shows up telling me stuff about file access, but I couldn't find any actually useful information on how to give me writing access. Help is greatly appreciated!
Could you paste all your script here please? In regular C# way you instantiate a StreamWriter, then write things to your file and then close it. With this piece of code, we cannot tell where your problem could lie, but if everything is okay with the writing then the problem should be that you don't close the file after writing to it.
Thank you for the response! As requested, I have linked the full script and the error message.SaveWorld Script
Answer by Bunny83 · Jan 02, 2019 at 09:40 AM
You have several misconceptions and errors here. First of all File.Create does no only create a file but opens the file for write operations. This method returns a file handle which you simply ignore. This file handle need to be closed before any other object / method / application can use this file.
Second File.WriteAllLines does more things at once than you might think. It does:
create the file if it doesn't exist yet.
open the file for write operations.
writes the given lines into the file (overwriting the pervious content if there is any).
closes the file handle once done.
However since you already opened a file handle before you called WriteAllLines, you can't open the file a second time and get a sharing violation.
So in your specific case you just want to get rid of your File.Create call which is wrongly used and unnecessary.
In general whenever you open file handles you should make sure you always close the handle once you're done with the file. Also keep in mind you can't have two handles to the same file(with a few exceptions). Closing a file handle does not necessarily require you to call "Close". It's much more common to use a "using section". You can use any object that implements the IDisposable interface in a using block. Once the using block is left, it automatically calls Dispose on that object. In case of a file stream this will close the file handle if it's still open.
Finally a warning when building a file path manually using string concat.
Different systems use different locations.
Unix / linux / MAC uses forward slashes while windows generally uses backslashes. In certain API calls windows does also accept forward slashes, but it shouldn't be taken for granted.
Application.dataPath generally points to the data path of your application. This path is not necessarily writable and doesn't necessarily represent a folder (In case of android this holds the file name of the APK file). Generally the dataPath is not meant as a writable folder. Use the persistentDataPath instead.
To combine path fragments, use System.IO.Path.Combine. It will take care about adding the right slash if necessary.
Thank you so much for the help! This answer cleared up so much! It took a bit of time to figure some stuff out, but my system now works perfectly!
Your answer
Follow this Question
Related Questions
How Do I Give Writing Permission of a Created File? 0 Answers
Distribute terrain in zones 3 Answers
How to tell android to open a file 0 Answers
Will System.IO.File work on Macs 2 Answers