As the title suggests, I would like to save a text file that is stored in a subfolder of the Assets folder in my project to a common directory such as “C:/” in Windows.
For some reason, My code works on the machine I build my program on, and saves the text files to the directory I specify, but when running the same build on another device, it does not save them to the directory I specified.
My text files are stored in the StreamingAssets folder for easy access to them via C# scripts.
Here is how I have implemented my script:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using UnityEngine;
public class WriteToFile : MonoBehaviour {
private string exportPath = "C:/";
const string textFile1Name = "message1.txt";
const string textFile2Name = "message2.txt";
const string textFile3Name = "message3.txt";
private string readTxt1 = Application.streamingAssetsPath + "/" + textFile1Name;
private string readTxt2 = Application.streamingAssetsPath + "/" + textFile2Name;
private string readTxt3 = Application.streamingAssetsPath + "/" + textFile3Name;
void Start() {
writeFile(textFile1Name, readTxt1);
writeFile(textFile2Name, readTxt2);
writeFile(textFile3Name, readTxt3);
}
private void writeFile(string fileName, string filePath) {
List<string> messageLines = File.ReadAllLines(filePath).ToList();
try {
if (!File.Exists(filePath)) {
File.WriteAllText(exportPath + fileName, filePath);
}
foreach (string line in messageLines) {
File.AppendAllText(exportPath + fileName, line + "
");
}
}
catch (IOException) {
Debug.Log(“Text output error!”);
}
}
}