- Home /
Create file in correct directory
Hey Great community of Unity Answers. When a new user account ("Actually work order") is created, it creates a directory for that given user. Then I want it to create a Text File in that directory with all the given information.
The Directory gets created just fine. With the help of Unity Answers, I was able to create and write to a Text File. However, I cant figure out, how to point the text file in the directory it just created. It just overwrites the same file over and over again. I tried an If statement but I am not a good programmer. This is my first real project. More of a learning experience than anything else. Here is my code so far for a work order. The code in question is at the bottom.
var workorderidGUI = "001";
var firstnameGUI = "First Name";
var lastnameGUI = "Last Name";
var custHomeNBRGUI = "000-000-0000";
var custCellNBRGUI = "000-000-0000";
var custStreetGUI = "";
var custCityGUI = "";
var custSTGUI = "";
var custZIPGUI = "";
var compDiag = "Enter Described Problem or Initial Troubleshooting.";
function OnGUI () {
workorderidGUI = GUI.TextField (Rect (885, 80, 40 , 20), workorderidGUI);
firstnameGUI = GUI.TextField (Rect (125, 80, 120, 20), firstnameGUI);
lastnameGUI = GUI.TextField (Rect (265, 80, 120, 20), lastnameGUI);
custHomeNBRGUI = GUI.TextField (Rect (135, 130, 100, 20), custHomeNBRGUI);
custCellNBRGUI = GUI.TextField (Rect (135, 160, 100, 20), custCellNBRGUI);
custStreetGUI = GUI.TextField (Rect (440, 130, 300, 20), custStreetGUI);
custCityGUI = GUI.TextField (Rect (440, 160, 100, 20), custCityGUI);
custSTGUI = GUI.TextField (Rect (580, 160, 30, 20), custSTGUI);
custZIPGUI = GUI.TextField (Rect (680, 160, 60, 20), custZIPGUI);
compDiag = GUI.TextField (Rect (60, 250, 360, 300), compDiag);
if (GUI.Button (Rect (25, 650, 120, 30), "Save Work Order")) {
//create Username
usernameGUI = firstnameGUI.Substring(0,1) + lastnameGUI.Substring(0,1) + workorderidGUI.Substring(0,1) + custHomeNBRGUI.Substring(0,1) + custCellNBRGUI.Substring(0,1) + custStreetGUI.Substring(0,1) + custCityGUI.Substring(0,1) + custSTGUI.Substring(0,1) + custZIPGUI.Substring(0,1);
//create Folder
if (!Directory.Exists ("C:/Data/" + firstnameGUI)) {
Directory.CreateDirectory ("C:/Data/" + firstnameGUI);
}
//Create Text file.
//save to textfile
System.IO.File.WriteAllText("C:/Data/data.txt", lastnameGUI + ", " + firstnameGUI + "," + workorderidGUI + "," + custHomeNBRGUI + "," + custCellNBRGUI + "," + custStreetGUI + "," + custCityGUI + "," + custSTGUI + "," + custZIPGUI);
}
}
If you want a better understanding of the application, I have included a alpha build: http://www.megaupload.com/?d=Z$$anonymous$$$$anonymous$$SOA7$$anonymous$$
Answer by almo · Jun 15, 2011 at 07:30 PM
Your WriteAllText statement says the path to write to is "C:/Data/data.txt". If you want to write the files somewhere else, you have to change that.
Like System.IO.File.WriteAllText("C:/Data/" + firstnameGUI + "/data.txt", bunch of data...);
I thought I tried that, but must of typed it wrong. that works! Thank you almo.
Answer by UnityNewbieTawin · Mar 26, 2014 at 06:19 PM
hello, newbie here
can you help me.. i got an error
No appropriate version of 'System.IO.File.WriteAllText' for the argument list '(String, String, boolean)' was found.
when i run your codes.. somebody help me -_-
The first step to solving such problems is always to check the documentation:
The first thing you should notice is that there are two versions of this method: the first version takes two strings as parameters. The second version takes three parameters (the first two are strings and the third is an Encoding object).
The error message is telling you that there is no method named WriteAllText that takes two strings and a boolean.
To fix your error, either pass two strings OR two strings and an Encoding type to the method named WriteAllText.
By the way, you should not create a new answer when you have a question... Please use comments in s$$anonymous$$d.
Also, if your app runs on the web, you CANNOT use this method for security purposes.
If you're not using a webplayer, make sure you import the System.IO namespace.
@jahroy: Since you pointed out the error, you might want to fix your answer as well ^_^.
To append text you can use AppendAllText
Answer by jahroy · Jun 15, 2011 at 07:31 PM
Try this in stead:
System.IO.File.WriteAllText("C:/Data/data.txt", someString, true);
You can add an extra boolean parameter to File.WriteAllText() to specify whether or not you would like to append to the file if it already exits.
That would be if you want to write all your data to the same file "C:/Data/data.txt".
If you want to write to different files, you need to change the file name like the other answer indicates.
Your answer
Follow this Question
Related Questions
My backpack script doubt 0 Answers
Search for files on android 2 Answers
Destorying despite IF Statement 1 Answer
parsing csv file 2 Answers
Script Performance Questions 3 Answers