Text file read line by line then convert to multiple strings
What i'm doing is saving things to a .txt file, each piece of information per line. (i.e. first name is on the first line, last name on the second line, email on the third line and so on) It then saves to a .txt file. What I want to do is read the .txt file and create a string for each line. So the first line would be one string and that string would have their first name in it. The second string would have their last name, the third string would have their email and so on. I also want to know if there is a better way for me to save data. Thanks.
Jan MontanoPosted Aug 31, 2009, 1:53 AM
{
loadFile.Filter = "Text files|*.txt";
loadFile.FileName = "";
DialogResult dialogResult = loadFile.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string getFile = loadFile.FileName;
StreamReader stream = new StreamReader(getFile);
string fileContent = stream.ReadToEnd();
string[] fileLines = fileContent.Split('\n');
stream.Close();
MessageBox.Show(fileLines[0], "First Name");
MessageBox.Show(fileLines[1], "Last Name");
MessageBox.Show(fileLines[2], "Email Address");
}
//file.Close();
}
Will GreenPosted Aug 30, 2009, 5:47 PM
Will GreenPosted Aug 30, 2009, 5:04 PM
Roei BarPosted Aug 30, 2009, 4:56 PM
that will give you all lines in a collection of strings :-)
dont forget to mark this as "Accepted Answer"