Help! Application.persistentDataPath File not found exception

Hi there, have a nice day I´m trying to save a textfile in persistent.datapath to running on android, but when I run the game in the program appears an error saying

FileNotFoundException: Could not find file “C:\Users\exito\AppData\LocalLow\com_bluewhile_prueba\visual3\Dialogue1.txt”.

I know that this error appears because some directories are missing and can not find them, but do not know how to create them, where they go, and what type of code is used to do

this is my fail code

string file;

        file = Application.persistentDataPath + "/Dialogue" ;
       
        string sceneNum = SceneManager.GetActiveScene().name;
        sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
        file += sceneNum;
        file += ".txt";

thanks for reading :slight_smile:

System.IO.Directory.Create(string.Format("{0}\{1}", application.persistentDataPath, "FolderName");

Application.persistantDataPath is always created based on the device you are on.

From what I’m seeing your saved file name and your load file name are not the same. I’m pretty sure that is why you’re getting the error.

1 Like

Note You should use System.IO.Path.Combine when combining file paths and file names.

1 Like

True

thanks for the answer, I did it this way, but I get an error in ** saying Unrecognized escape sequence

System.IO.Directory.CreateDirectory(string.Format("{0}\{1}"), Application.persistentDataPath, "Text");

Do this intead:

System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Application.persistentDataPath, "Text"));

You had a closing paren in the wrong place, but use Path.Combine instead.

Hi, I finally decided to use Streaming Asset to detect the text file, using android path and a WWW class, this is what is in the folder StreamingAsset, but its not working

Imgur

each file belongs to a scene dialog, I use StreamReader to read every line of dialogue, I do not know if the error is because is missing using Unity Editor in the beginning or something like that, and this is the code , thanks for reading :frowning:

 IEnumerator Startt()
    {
        string file;
        //captures the current scene number
        string sceneNum = SceneManager.GetActiveScene().name;
        sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
        //the path where is the file Dialogue
        string filePath = "jar:file://" + Application.dataPath + "!/assets/" + "Dialogue";
        //Dialogue + SceneNum so the result is Dialogue1
        filePath += sceneNum;
        //the result is Dialogue1.txt so this way detects that its a text file
        filePath += ".txt";

        //I put int a www class and I make it a string to be detectable
        WWW www = new WWW(filePath) ;
        yield return www;
        file = www.text;


        lines = new List<DialogueLine>();
        Debug.Log(file);

        LoadDialogue(file);

    }

Why are you not using

if you are sticking files into the streamingassets folder? You should look at the unity example in that link since you are loading a local file, you need the ReadAllText portion.

thanks for your continued support
I found this

http://docs.unity3d.com/Manual/StreamingAssets.html

and there says that the path of StreamingAsset on android is this path = “jar:file://” + Application.dataPath + /assets/"; and is not necessary System.IO.Path. then I do not know how to configure the ReadAllText to have consistency

Do it like in the example they have in the link I showed you. Don’t use your link. Your path should be Application.streamingAssetsPath + the file name. You should use the combine like they are doing.

Pass this to the File.ReadAllText. the System.IO is because they don’t have the using. You can put a using in instead or just do it like they show you.

sorry for the wait, I put the code as you suggested it to me but I had this error
NullReferenceException: Object reference not set to an instance of an object in the streamreader line 78
```csharp
**public void LoadDialogue(string filename)

{
    string line;
    StreamReader r = new StreamReader(filename);

    using (r)
    {
        do
        {
            line = r.ReadLine();
            if (line != null)
            {
                string[] lineData = line.Split(';');
                if (lineData[0] == "Player")
                {
                    DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, "");
                    lineEntry.options = new string[lineData.Length - 1];
                    for (int i = 1; i < lineData.Length; i++)
                    {
                        lineEntry.options[i - 1] = lineData[i];
                    }
                    lines.Add(lineEntry);
                }
                else
                {
                    DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), lineData[3]);
                    lines.Add(lineEntry);

                }

            }
        }
        while (line != null);
        r.Close();


   
    }
}

public string GetPosition(int lineNumber)
{
    if (lineNumber < lines.Count)
    {
        return lines[lineNumber].position;
    }
    return "";
}

public string GetName(int lineNumber)
{
    if (lineNumber < lines.Count)
    {
        return lines[lineNumber].name;
    }
    return "";
}

public string GetContent(int lineNumber)
{
    if (lineNumber < lines.Count)
    {
        return lines[lineNumber].content;
    }
    return "";
}

public int GetPose(int lineNumber)
{
    if (lineNumber < lines.Count)
    {
        return lines[lineNumber].pose;
    }
    return 0;
}

public string[] GetOptions(int lineNumber)
{
    if (lineNumber < lines.Count)
    {
        return lines[lineNumber].options;
    }
    return new string[0];
}**

```

I don’t know where line 78 actually falls based on the code you showed, so I can’t tell you what it is pointing at.

But a null reference error means something wasn’t initialized and you’re trying to use it.

ok I think I’ve made a little progress, i put all streamreader code in the IEnumerator and don’t shows errors in the console but in my phone still does not show the dialogue, I have tried several types of combinations that work well in unity as a void without the WWW but when I put IEnumerator and WWW in my phone does not work, this is the condition for shows the dialogue and in other settings is write access external sd card

 if (Input.touchCount >1 )
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began && playerTalking == false)
        {
            ShowDialogue();
            lineNum++;
        }

This is one of the codes I tried

        string result = "";
        string sceneNum = SceneManager.GetActiveScene().name;
        sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
        string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Dialogue" + sceneNum + ".txt");
        if (filePath.Contains("://"))
        {
            WWW www = new WWW(filePath);
            yield return www;
            result = www.text;

            Debug.Log(result);

          
        }
        else
        {
            result = System.IO.File.ReadAllText(filePath);
        }

        string line;
        StreamReader r = new StreamReader(result);

this is another one

string filePath = Path.Combine(Application.streamingAssetsPath, "Dialogue" + sceneNum + ".txt");

PD : "Don’t give up me, I feel I’m close to doing

In the hopes that this info is helpful: For my specific situation I am doing the following for macos and ios

using "URI=file://{path}"; instead of path = “URI=file:{path}”;

I am copying all files from streaming assets folder to my persistent data folder before trying to read from them. I had to do that specifically to read from database files due to how db files (sqlite) are interacted with during runtime.