I want to save the text from several textboxes to a textfile. Then once I save it I want to open it and all the text go to their appropiate textbox.

With my code when I click save it saves the file. I open the file from my computer (not my program) to see if it really saved. Only the first textbox got saved not the second one. There was just a space then the text of the first box.

I then wanted to test what would happen if it opened. So I opened it and it was the same as the text file. But the text of the first box went to the second box and it was empty in the first box.

Here is the open code Im working with:

private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open a File";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Text Files|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
                {
                    textBox1.Text = reader.ReadToEnd();
                    textBox2.Text = reader.ReadToEnd();
                }
            }
        }

And here is the save code Im working with:

private void button7_Click(object sender, EventArgs e)
        {
           
            StreamWriter sw = new StreamWriter("C:\\test.txt");

          
            sw.WriteLine(textBox1.Text);
            sw.WriteLine(textBox2.Text);

            sw.Close();


        }

Change lines 12 and 13 in your first code block to read

textBox1.Text = reader.ReadLine();
textBox2.Text = reader.ReadLine();

I edited the code. In textbox1 I put hi, and in textbox2 I put hello. I saved the file. I then went manually to the C drive to find the file. I then opened it and here is how the file looked like:


hi

Same issue with the textfile. But this time when I open it instead of the text appearing in the second textbox it goes in the first

Are you sure? How come your works and not mine if I followed all your instructions exactly?

Also, are there any other ways to save?

Here is the sample I'm using

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void ClearButton_Click(object sender, EventArgs e) {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
        }

        private void SaveButton_Click(object sender, EventArgs e) {
            using (StreamWriter sw = new StreamWriter("myFile.txt")) {
                sw.WriteLine(textBox1.Text);
                sw.WriteLine(textBox2.Text);
                sw.Flush();
            }

        }

        private void LoadButton_Click(object sender, EventArgs e) {
            using (StreamReader sr = new StreamReader("myFile.txt")) {
                textBox1.Text = sr.ReadLine();
                textBox2.Text = sr.ReadLine();
            }
        }
    }
}

If your code matches this, then I suspect you are selecting the wrong file when you tell it to load the data. Change the name of the file in your save section and then try loading it.

I copied it but now its giving me an errro: System.IO.StreamWriter' does not contain a constructor that takes 0 arguments

It shouldnt say that cause we already gave it an argument.

Strangely the error went away but now its not saving or opening. I assumed it was cause there was no openfile box orsavefile box opening so I added it but its still not working heres the code:

private void btnSave_Click(object sender, EventArgs e)
        {

            saveFileDialog1.InitialDirectory = "C:";
            saveFileDialog1.Title = "Save a File";
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Text Files|*.txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {


                using (StreamWriter sw = new StreamWriter("myFile.txt"))
                {
                    sw.WriteLine(textBox1.Text);
                    sw.WriteLine(textBox2.Text);
                    sw.Flush();
                }

            }

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open a File";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Text Files|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                using (StreamReader sr = new StreamReader("myFile.txt"))
                {
                    textBox1.Text = sr.ReadLine();
                    textBox2.Text = sr.ReadLine();
                }
            }

        }
This article has been dead for over six months. Start a new discussion instead.