c# writing user input from form to text file using windows a
How do i write the user input from my form (windows based application) to a text file?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 23, 2013, 12:38 PM
Otherwise it overwrites the file with the new stuff.
Of course, File.ReadAllText or File.ReadAllLines can only read the file if it already exists and I assume that's what you were missing with your own code :)
ICE QUEENPosted May 24, 2013, 7:31 AM
ICE QUEENPosted May 24, 2013, 7:22 AM
VulpesPosted May 24, 2013, 7:14 AM
I'll help if I can though I do have other stuff to do today.
May be it would be better if you got some sleep first as a clear head is needed for programming.
ICE QUEENPosted May 24, 2013, 7:04 AM
VulpesPosted May 24, 2013, 7:03 AM
So, if you'd be more comfortable saving them as you go along, I'd do that.
All you need is a call to a SavePersons method after each change to the List
Incidentally, as this thread is getting very long, I'd start a new one so we don't have to do so much scrolling!
ICE QUEENPosted May 24, 2013, 6:45 AM
VulpesPosted May 24, 2013, 6:38 AM
However, I don't have any problem with how you want to do it which is what we started off with. I just need to be clear how you want it doing.
The code I gave in my previous post could be called at any time to save the current state of the List
Rather than mess around, trying to append stuff to the file, it might be easiest just to overwrite the file every time there's a change - whether this be a new person or a change to an existing one.
ICE QUEENPosted May 24, 2013, 6:26 AM
VulpesPosted May 24, 2013, 6:21 AM
1. Load the List
2. Add new Person objects to, or amend existing Persons in, the List
3. Save the current state of the List
If we're not doing that then what are we doing - saving every change to the file as it occurs?
ICE QUEENPosted May 24, 2013, 6:15 AM
I am under the impression that i need to write to file sooner then that closing the app,
VulpesPosted May 24, 2013, 6:10 AM
ICE QUEENPosted May 24, 2013, 6:01 AM
ICE QUEENPosted May 24, 2013, 5:59 AM
ICE QUEENPosted May 24, 2013, 5:56 AM
VulpesPosted May 24, 2013, 5:51 AM
ICE QUEENPosted May 24, 2013, 5:38 AM
ICE QUEENPosted May 24, 2013, 2:53 AM
Bhabani prasad DashPosted May 24, 2013, 2:48 AM
ICE QUEENPosted May 24, 2013, 1:41 AM
ICE QUEENPosted May 24, 2013, 1:41 AM
ICE QUEENPosted May 23, 2013, 8:00 PM
VulpesPosted May 23, 2013, 7:56 PM
ICE QUEENPosted May 23, 2013, 7:50 PM
ICE QUEENPosted May 23, 2013, 7:46 PM
ICE QUEENPosted May 23, 2013, 7:37 PM
ICE QUEENPosted May 23, 2013, 7:31 PM
VulpesPosted May 23, 2013, 7:21 PM
ICE QUEENPosted May 23, 2013, 5:34 PM
VulpesPosted May 23, 2013, 4:33 PM
It doesn't want to be included in the Person class itself.
Nor is there any point in creating a new class, just to house methods.
You can either put them in the Person class itself or in the form - the latter might be best seeing what you need to do.
Here's a method to get a list of people in a given country. Notice that, if there are no persons in that country, an empty List would be returned (Count = 0).
private List
{
List
foreach(Person p in persons)
{
if(p.Country == country) list.Add(p);
}
return list;
}
ICE QUEENPosted May 23, 2013, 4:22 PM
VulpesPosted May 23, 2013, 4:16 PM
ICE QUEENPosted May 23, 2013, 4:09 PM
ICE QUEENPosted May 23, 2013, 3:51 PM
VulpesPosted May 23, 2013, 3:40 PM
So, if you used an array, you'd need to impose a maximum on the size of the file.
If you used an ArrayList or (better a List
as these collections can automatically increase their size as needed.
What you could do is to do everything in memory using one of these collections.
You'd only then need to load the file when the program starts and save it when the program ended.
If you did this, you'd need a Person class:
class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Country {get; set;}
// any others
public Person(string fName, string lName) // constructor if only names given
{
FirstName = fName;
LastName = lName;
Country = "";
}
// add another constructor to initialize all fields
}
ICE QUEENPosted May 23, 2013, 3:27 PM
VulpesPosted May 23, 2013, 3:11 PM
Terrance CPosted May 23, 2013, 2:30 PM
ICE QUEENPosted May 23, 2013, 2:26 PM
ICE QUEENPosted May 23, 2013, 2:22 PM
1. Create a new Person record (based on user input from the form) - only the name fields are mandatory so they can come back later to fill in the rest of the form.
2. List ALL the names in the system. (that was obviously saved in 1 above)
3. List the names of those people whose names contain a given string(user input).
Eg: given string = "Tim", all Tim's will show: eg. "Tim Taylor", "Jack Tims", "Tomothy Smith".
4. List the people from a given country
5. Bring up the Person record by clicking the name of the person from the Person list in 2,3 & 4 above and allow the user to modify the record......
I still have alooot today :___ (
so i really appreciate the help!!!
VulpesPosted May 23, 2013, 2:12 PM
ICE QUEENPosted May 23, 2013, 2:11 PM
What about the use of instance concstructors in this case?
ICE QUEENPosted May 23, 2013, 2:03 PM
ICE QUEENPosted May 23, 2013, 2:00 PM
user clicks submit
if the file already exists
append to that file
else
create new file
so far i've got :
private void submitButton_Click (object sender, EventArgs e)
{
lNameTextBox_Validated(this, e);
fNameTextBox_Validated(this, e);
createNewPerson();
}
private void createNewPerson()
{
using (StreamWriter sw = new StreamWriter(dataPath)
if(File.Exists) //this shows error: cannot convert method group exists to non delegate type bool????? HELP!
}
VulpesPosted May 23, 2013, 1:38 PM
ICE QUEENPosted May 23, 2013, 1:29 PM
now i am stuck as to how i can actually implement this....
I have a windows based application form for a new employee, with the usual fields, Name, Address etc. After the user has filled in the form, i need to write (copy) that to a text file to SAVE the info...
Any help will be MUCH APPRECIATED!!!!
VulpesPosted May 23, 2013, 1:15 PM
However, if you're use StreamReader or StreamWriter, then I agree that you need to be careful to close the file afterwards or utilize the 'using' statement instead which does it for you.
Terrance CPosted May 23, 2013, 1:09 PM
using (StreamWriter sw = new StringWriter(filename,true))
{
sw.WriteLine("Some text...");
sw.Flush();
}
The using statement will make sure that your file is close successfully.
ICE QUEENPosted May 23, 2013, 12:30 PM
ICE QUEENPosted May 23, 2013, 12:17 PM
i have been trying to use:
string[] lines;
if (File.Exists(@"c:\test.txt"))
{
lines = File.ReadAllLines(@"c:\test.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
File.WriteAllLines(@"c:\test.txt", lines);
}
Console.Read();
}
}
}
I dont get any errors but cant seem to find the file?????
VulpesPosted May 23, 2013, 12:08 PM
string userInput = textBox1.Text + "\r\n" + textBox2.Text + "\r\n" + textBox3.Text;
System.IO.File.WriteAllText("somefile.txt", userInput);