- Home /
Create Text File
I am trying to create an example of Unity creating Text files / directories. Can someone point to me the correct function I need to call to have unity create a new file called "Data" or something?
This tool is meant to take a number of GUI textfields, be saved, and then loaded for later use. so simple Text files on the local harddrive is all I need.
Code I have:
var firstnameGUI : First Name;
var lastnameGUI : Surname;
function OnGUI () {
firstnameGUI = GUI.TextField (Rect (25, 80, 150, 30), firstnameGUI);
lastnameGUI = GUI.TextField (Rect (25, 115, 150, 30), lastnameGUI);
if (GUI.Button (Rect (25, 350, 120, 30), "Save User")) {
//create Username
usernameGUI = firstnameGUI.Substring(0,1) + lastnameGUI.Substring(0,1);
//create Folder
if (!Directory.Exists ("C:/Data/" + usernameGUI)) {
Directory.CreateDirectory ("C:/Data/" + usernameGUI);
}
//Create Text file.. but dont know how.. How to create the Text file to save to.
//save to textfile
File.WriteAllText(firstnameGUI, lastnameGUI);
}
}
Answer by CHPedersen · Jun 15, 2011 at 07:09 AM
You're very close. :)
File.WriteAllText does take two arguments like you've supplied, but the first one is the filepath. The text you want to write is the second one. So you need something like,
System.IO.File.WriteAllText("C:\blahblah_yourfilepath\yourtextfile.txt", firstnameGUI + ", " + lastnameGUI);
That is, assuming you want your values comma-separated in the file. You can format the string that gets written in whatever way you like.
Just a heads up for anyone looking at this: The character \ is an escape character. So you will either need to use \\ or / in your file path ins$$anonymous$$d.
Answer by JITENDRA-RAJ · Sep 26, 2014 at 02:02 PM
You try with this it works....
System.Text.UnicodeEncoding encod = new System.Text.UnicodeEncoding(); byte[] byteData = encod.GetBytes(Downloadchat);
System.IO.FileStream oFileStream = null;
oFileStream = new System.IO.FileStream("D:\\chats.txt", System.IO.FileMode.Create);
oFileStream.Write(byteData, 0, byteData.Length);
oFileStream.Close ();
Answer by grimmy · Oct 05, 2011 at 08:20 AM
Doesnt work for me.? I get an error:
unexpected char: 'U'.
When I do
System.IO.File.WriteAllText("C:\Users\Simon\Desktop\exampleSaveData.txt", stringToPost);
I have no clue why its erroring on the first letter of the first folder in the path string..?? I tried rebooting Unity but I get the same error... Am I going mad??
If you have a question of your own, start a new Question. Unity Answers doesn't work like a forum - this isn't a thread of discussion. This is supposed to be 1 Question, followed by x answers to that question.
Okay no worries, will do. I figured as it's so tightly related that any problems arising from the solution offered should be viewable in the same place. Cheers. BTW-the answer to my problem is that you need to make sure you are running a standalone platform to use the function described above ($$anonymous$$e was set to webplayer.)
You should not have posted your problem as an answer, Either start a new question or post it as a comment but your problem did help me. And I agree it is nice so see the solution in the same place, just post it as a comment next time to be considerate. People get emails when you post answers on something they are watching. =)
I think to make a file in local disk C you have need to run application as ad$$anonymous$$istrator.
Post your own question as a new question, not an answer >:/
Your answer
Follow this Question
Related Questions
Write file after build 1 Answer
How to create and write a text file after compile/build 0 Answers
parsing csv file 2 Answers
Read txt file from Android Downloads file 1 Answer
Executing code from a text file and writing changes to it 1 Answer