- Home /
Compatibility level and writing *.txt files
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: 1) I use Unity free 2) API compatibility level in player preferences: .NET 2.0 (not subset) 3) 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);
}
}
}
add Debug.Log() to this method and check what happened and input prameters for correct data. also, did you already check the client's log after saving fails?
Answer by Bluk · Aug 05, 2012 at 11:30 AM
You should have this exception in your output_log.txt in the standalone_name_Data folder:
NotSupportedException: CodePage 1252 not supported
at System.Text.Encoding.GetEncoding (Int32 codepage) [0x00000] in <filename unknown>:0
It is due to a missing .dll
See here: http://answers.unity3d.com/questions/42955/codepage-1252-not-supported-works-in-editor-but-no.html
Cheers.