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

Recommended Answers

All 11 Replies

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();

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 ?

Sure, just include the "Export to XML" feature for the user if they really want that.

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

Try WriteLine instead of Write. Or try writing "\n" for a new line.

writeline work perfect thanks

No problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.