Sub-folders in Application.dataPath

Bit of a weird problem here.
I’m not even sure if I’m using thiss correctly, but I’m pretty sure I am.

This works in the Unity Editor but when I build to executable andd run it from there, the folder and file are never created.

System.IO.File.WriteAllText(Application.dataPath + "/Engine Settings/EnginePrefs.json", saveprefs);

For some reason, this workss fine in the build when I remove the sub-folder and just write it as such:

System.IO.File.WriteAllText(Application.dataPath + "/EnginePrefs.json", saveprefs);

Iss there something I am doing wrong here in which the folder/file is not being created? Do I need a different approach for a build version?

Maybe it has something to do with the space in the folder name?

2 Likes

Not too sure. One thing I’ve done is in the start function check to see if directory.Exist and if it doesn’t then do directory.create. From my experience if the directory exists but the file doesn’t, the writealltext generates the file anyways.

2 Likes

The documentation for writealltext certainly does NOT assert this. Assume it at your own peril.

Reading this post…

https://stackoverflow.com/questions/2955402/how-do-i-create-directory-if-it-doesnt-exist-to-create-a-file

…leads me to think perhaps it does NOT.

1 Like

Right, System.IO.File.WriteAllText does NOT create the folder in the specifed file name. If you just look at the documentation and check the possible exceptions this method can throw you will find that it will actually throw a
DirectoryNotFound exception if the path doesn’t exist or is invalid.

2 Likes