Hi, i have a form with data and I am tryin to save and load data using a texfile, any suggestion how I could start this as Im not too sure, thanks

Yup

StreamWriter fileOut = new StreamWriter("file.txt");
fileOut.WriteLine("stuff goes here");
fileOut.Close();

then...

string strData = "";
StreamReader fileIn = new StreamReader("file.txt");
while(!fileIn.EndOfStream)
{
   strData = fileIn.ReadLine();
}
fileIn.Close();

Edited 5 Years Ago by thines01: Added mor ecode

thines has a good suggestion.

If you are going to be holding many different pieces of data I would recommend delimiting the data in some fashion or otherwise structuring your file so that there is a "table of contents" so to speak so you can find where certain data is.

Unless it's absolutely necessary to store the data in a text file, you could serialize your data which might be easier.

I would recommend delimiting the data in some fashion or otherwise structuring your file so that there is a "table of contents" so to speak.

Zachattach05 has a good point look into using Xml to store data, you can structure your data using tags/ nodes and child tags/ nodes to add more structure to your data.

It's a good idea aswell because you can use this data across different DBMS'

XML works, but I avoid it like the plague.

XML files are bloated for the sake of "human readability."

I always use straight up binary serialization. You could always include a "export to XML" feature :)

pretty hardcore going all the way down to binary, it would take up a lot less space but would binary be as re-usable and readable by other users as xml ?

Yup

StreamWriter fileOut = new StreamWriter("file.txt");
fileOut.WriteLine("stuff goes here");
fileOut.Close();

then...

string strData = "";
StreamReader fileIn = new StreamReader("file.txt");
while(!fileIn.EndOfStream)
{
   strData = fileIn.ReadLine();
}
fileIn.Close();

thank you, I have the data in a textbox, so where in the code would I tell the program the name of this textbox to get the data from, and the path of the file, thanks a lot

i figured it out, could someone tell me how I would insert a new line

objWriter.Write(txtName.Text);
objWriter.Write(txtTelephone.Text);
objWriter.Write(txtAddress.Text);

atm it saves but its all squashed together. thanks

This question has already been answered. Start a new discussion instead.