0

I am trying to get text file read/write access at Unity for iOs project. What I did was

        //Read
        StreamReader reader = new StreamReader(Application.persistentDataPath + "/record.txt"); 
        string record = reader.ReadToEnd ();
        string[] records = record.Split(new string[] { " " }, StringSplitOptions.None);
        firstname = records [0]; lastname = records [1]; loginName = records [2]; password = records [3];
        reader.Close();
        email.transform.GetComponent<InputField> ().text = loginName;
        pwd.transform.GetComponent<InputField> ().text = password;


        //Write

        File.WriteAllText(Application.persistentDataPath + "/record.txt", String.Empty);
        //File.Create (Application.persistentDataPath + "/record.txt").Close ();
        StreamWriter writer = new StreamWriter (Application.persistentDataPath + "/record.txt", true);
        writer.WriteLine (Login.firstname + " " + Login.lastname + " " + Login.loginName + " " + Login.password);
        writer.Close ();

Then I include the file to Build Phases -> Copy Bundle Resources, so the file is inside the project. But when I run the app inside the device, I can't read file and got message as

IsolatedStorageException: Could not find file "/var/mobile/Containers/Data/Application/D48E51C7-F2EE-48CF-9B84-6602CE135EBC/Documents/record.txt".
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.File.OpenRead (System.String path) [0x00000] in <filename unknown>:0 

What could be wrong?

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

0

Putting text file in Resources folder is not a good practice because Unity will always try to serialize everything you put in that folder at build time. Instead copy your file into StreamingAssets folder then use

StreamReader sr = new StreamReader(Path.Combine(Application.streamingAssetsPath,"text.txt"));

This will work just fine. Note : for files that you don't want Unity to touch at build time put them in StreamingAssets.

buffalo94
  • 159
  • 5