- Home /
How to create and write a text file after compile/build
I've been struggling with this for quite some time now and I've already posted a few questions but didn't get the answer I was looking for, and that's probably because I didn't ask the question correctly.
I'm trying to store some gamedata in a text file and it all works fine when I'm testing it in the editor but as soon as I try to do it after building nothing happens, no files are created and no text is written. This is what I'm doing :
FileInfo f = new FileInfo(Path.Combine(Application.persistentDataPath, "data.txt"));
StreamWriter w;
if(!f.Exists)
{
w = f.CreateText();
}
else
{
f.Delete();
w = f.CreateText();
}
foreach (string s in text)
{
w.WriteLine(s);
}
w.Flush();
w.Close();
Now 's' is a List that contains the data I want to write to the file. I've tried working with a TextAsset too but I haven't gotten that to work either after building the game.
If you're on windows check C:\Users\yourusername\AppData\Local\VirtualStore (make sure you replace yourusername with your username). Programs that don't have permission to a directory can have their path rerouted there.
Your answer
Follow this Question
Related Questions
Create Text File 3 Answers
IOException: Sharing violation on path. Please Help! 2 Answers
Write file after build 1 Answer
Unity does not finish write file (filecontent suddenly ends) 1 Answer
parsing csv file 2 Answers