I am using a script to create a text file in “my documents” folder. It works perfectly while testing it in the Editor, but when I build and run the standalone it doesn’t work anymore.
Facts:
- I use Unity free
- API compatibility level in player preferences: .NET 2.0 (not subset)
- There are no compilation errors or warnings
The funny thing is that my game also has scripts that use: System.IO; System.Reflection; and System.Runtime.Serialization; to create “save game files” and it works just fine.
What could be possibly going wrong?
Here is the script to create *.txt files thank you in advance:
using System;
using System.IO;
using System.Text;
public class WriteTextFile
{
static string mydocpath;
static string txtList;
static StringBuilder sb = new StringBuilder ();
public static void WriteFile ()
{
mydocpath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
txtList = Directory.GetFiles (mydocpath, "*.txt");
foreach (string txtName in txtList) {
using (StreamReader sr = new StreamReader(txtName, Encoding.GetEncoding(1252))) {
sb.AppendLine (txtName.ToString ());
sb.AppendLine ("= = = = = =");
sb.Append (sr.ReadToEnd ());
sb.AppendLine ();
sb.AppendLine ();
}
}
using (StreamWriter outfile = new StreamWriter (mydocpath + @"\AllTxtFiles.txt", false, Encoding.GetEncoding(1252))) {
string temp = sb.ToString ();
outfile.Write (temp);
}
}
}