How to save a text file stored in the project's assets to a directory in Windows

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!”);
}
}
}

Try the following:

using System;
using System.IO;

using UnityEngine;

using static System.Net.Mime.MediaTypeNames;

public class WriteToFile : MonoBehaviour
{
	/// <summary>
	/// The path to export files to.
	/// </summary>
	[SerializeField] private string _exportPath = "C:/";

	/// <summary>
	/// The name of the text files that can be exported.
	/// </summary>
	private readonly string[] _textFileNames = new string[]
	{
		"message1.txt",
		"message2.txt",
		"message3.txt",
	};

	/// <summary>
	/// An array of paths that's populated at runtime with the full paths of the files from the _textFileNames array.
	/// </summary>
	private string[] _textFilePaths;

	private void Start()
	{
		// Initialize the _textFilePaths array to be of the same length as the _textFileNames array.
		_textFilePaths = new string[_textFileNames.Length];

		// Populate it using a for loop.
		for (int i = 0; i < _textFileNames.Length; i++)
		{
			_textFilePaths <em>= Path.Combine(Application.streamingAssetsPaths, _textFileNames*);*</em>

* }*

* // Copy all the files from the textFilePaths array.
for (int i = 0; i < textFileNames.Length; i++)
_
{*

WriteFile(textFilePaths, textFileNames*);*
* }*

* }*_

* ///

*
* /// Copies a the contents of the file stored at to .*
* ///
*
* /// The file to read from.*
* /// The file to write to.*
* private void WriteFile(string source, string target)*
* {*
* try*
* {*
* // Write the file as a text file.*
* File.WriteAllText(Path.Combine(exportPath, "{target}.txt"), File.ReadAllText(source));*</em></em></em> <em><em>_* }*_</em></em> <em><em>_* // Catch ANY exception and log it.*_</em></em> <em><em>_* catch (Exception exception)*_</em></em> <em><em>_* {*_</em></em> <em><em>_* Debug.Log(“Error while copying text from: ‘{source}’ to: ‘{target}’. {exception}”);*
* }*
* }*
}