Question by
DaresFireson · May 15, 2019 at 06:27 AM ·
c#fileiodatapath
[SOLVED]File don't save in persistentDataPath
The txt file refuse to save in persistentDataPath and it get saved in project folder or in game folder if build
if (!Directory.Exists(Application.persistentDataPath + "/Maps"))
Directory.CreateDirectory(Application.persistentDataPath + "/Maps");
if (!File.Exists(path + "Level01.txt"))
{
TextAsset l01 = Resources.Load("Level01") as TextAsset;
string data = l01.text.Replace(Environment.NewLine, string.Empty);
string[] dataArray = data.Split('-');
//File.WriteAllText(path + "Level01.txt", data);
StreamWriter sr = new StreamWriter(path + "Level01.txt", true);
for (int i = 0; i < dataArray.Length; i++)
{
if(i == dataArray.Length -1)
sr.WriteLine(dataArray[i]);
else
sr.WriteLine(dataArray[i] + "-");
}
sr.Close();
}
If I change to dataPath it refuse to create folder. Anyone have any idea what is happening ? I use the latest version of unity 2019
Comment
Best Answer
Answer by Jack-Mariani · May 15, 2019 at 06:37 AM
It seems you're not saving the string path. The best approach is to use a string and call just one time Application.persistentDataPath
.
Try changing this:
if (!Directory.Exists(Application.persistentDataPath + "/Maps"))
Directory.CreateDirectory(Application.persistentDataPath + "/Maps");
Into this:
path = Directory.Exists(Application.persistentDataPath + "/Maps";
if (!Directory.Exists(path ))
Directory.CreateDirectory(path);
if (!File.Exists(path + "/Level01.txt"))
...
well it seem unity dont like
path = Application.persistentDataPath + "/$$anonymous$$aps/";
I used your solution but without Directory.Exist and it save right now. Thank you