[Resolved] Can't write in Application.persistentDataPath (no error sent)

Hello,

i am using unity 2017.1.0f3 and i am stuck on a problem. I can’t write in the Application.persistentDataPath folder and i catch no error

 public void CreateFile()
    {
      
        StreamWriter file;
      
        string fullPath = Application.persistentDataPath + "/test.txt";

        try
        {
            if (!File.Exists(fullPath))
            {
                file = File.CreateText(fullPath);
                file.WriteLine("test");
                file.Close();
            }
            else
            {
                File.WriteAllText(fullPath, "test");
            }

        }
        catch (Exception e)
        {
        }

    }

It is one of the simplest test i’ve done but it’s not working. The app is trying to write in /storage/emulated0/Android/data/com.company.import/files/test.txt but no file is created.

I looked a little on my phone and noticed that an other app used an other path

/storage/3064-3934/Android/data/com.company.import/files/test.txt

I used this path in my script and it worked (so it’s not a permission issue). But i need a path that works on the majority of the devices.

Can somone help me on that ?

Phone used for testing : Samsung S5 SM-G900F android 6.0.1

Application.persistantDataPath is not the same on each device, but it should always pick a path that it can use.

Use log viewer to view console stuff on your device after building out or use logcat to verify you aren’t getting any unseen issues.

https://www.assetstore.unity3d.com/en/#!/content/12047 log viewer.

1 Like

Hum, thanks that helped me.

The problem was as stupid as i though. The file was indeed created (i checked a file.exist(fullpath) with the log and it returned true.

I checked … and the file was in Android/com.company.app/files, but not on the SD card as i though it would be … it was on the device storage

I feel dumb now

Thanks again for your help

2 Likes

Don’t feel dumb on this. This one can be a bit weird sometimes, which is why log viewer is so helpful. Glad you got it sorted out!