Search Unity

Sub-folders in Application.dataPath

Discussion in 'Scripting' started by Studio_Akiba, Aug 7, 2020.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    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.

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. 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?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Maybe it has something to do with the space in the folder name?
     
    Joe-Censored and JustJessop like this.
  3. JustJessop

    JustJessop

    Joined:
    Apr 19, 2019
    Posts:
    4
    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.
     
    Studio_Akiba and PraetorBlue like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    PraetorBlue likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,999
    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.
     
    Kurt-Dekker and PraetorBlue like this.