I have created a file now I want to read it back into the program.When I click button2 I want it to read the file and display it in label6.text
public void writetext()
{
using (TextWriter writer = File.AppendText("filename.txt"))
{
writer.WriteLine("First name, {0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text);
MessageBox.Show(String.Format("First Name,{0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text));
}
}
public void reset()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
maskedTextBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
private void button2_Click(object sender, EventArgs e)
{
File.ReadAllLines("filename.txt");
label6.Text = ("filename.text");
}
}
}
File.ReadAllLines(filename) will return a string[] (one string for each line) – in your code you aren’t storing the returned string[] from ReadAllLines(). Also, ReadAllLines() will close the stream for you I believe.
Without making design suggestions on what you have, you would need to do something like this to get what you want:
That would join the array of lines separated by the newline character appropriate for your region. It then assigns the result string to the label.