Tom
Tom

Reputation: 19

Saving multiple pieces of form input to a file

What would be the best way to save a windows form to a file with a few text boxes collecting user input. I using this at the moment:

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, tB1.Text);
                File.WriteAllText(saveFileDialog1.FileName, tB2.Text);
            }

This is fine for saving the input from the first text box but when it comes to the other it wont save the data entered.

Upvotes: 2

Views: 2072

Answers (6)

spajce
spajce

Reputation: 7082

There we go, use Encoding to Append all the string.

    private void button1_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string line = string.Format("{0},{1}"
            , textBox1.Text
            , textBox2.Text);
            File.AppendAllText(saveFileDialog1.FileName, line, Encoding.GetEncoding(1252));
        }
    }

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

If it was me I would use the StreamWriter / StreamReader Classes since they have WriteLine and Readline methods respectively.

i.e. something like this

private void button1_Click(object sender, EventArgs e)
{
    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
        {
            sw.WriteLine(tB1.Text);
            sw.WriteLine(tB2.Text);
            sw.WriteLine(tB3.Text);
            sw.WriteLine(tB4.Text);
            sw.Close();
        }

   }

}

private void button2_Click(object sender, EventArgs e)
{
    if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
        {
            tB1.Text = sr.ReadLine();
            tB2.Text = sr.ReadLine();
            tB3.Text = sr.ReadLine();
            tB4.Text = sr.ReadLine();
            sr.Close();
        }
    }

}

Upvotes: 1

Ravi Y
Ravi Y

Reputation: 4376

File.WriteAllText is probably bad because it overwrites your content.

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Instead go with File.AppendAllText which

Appends the specified stringto the file, creating the file if it does not already exist.

Upvotes: 1

krillgar
krillgar

Reputation: 12805

The best way would probably be to create a method in your form that will return a string with all of the values from the TextBoxes into whatever format you want. Something like this would work:

File.WriteAllText(saveFileDialog1.fileName, OutputUserInfo());

Then inside OutputUserInfo() you can have whatever formatting to the data that you want so you can understand what they put in.

Edit Example of OutputUserInfo()

private string OutputUserInfo() {
    return  "First Name: " + tbFirstName.Text + Environment.NewLine +
            "Surname: " + tbSurname.Text + Environment.NewLine +
            "Address" + tbAddress.Text + Environment.NewLine;
            // Just keep adding whatever you want on here.
            // Add the descriptions if you want, it will probably help
}

You could also have this in different formats (CSV, or whatever). But if you're just doing a plain text file, this could be easiest. It is up to you though.

Upvotes: 1

John Woo
John Woo

Reputation: 263683

how about concatenating the two textboxes? for clarity,

string forSaving = tB1.Text + "\n" + tB2.Text;
File.WriteAllText(saveFileDialog1.FileName, forSaving);

or

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + tB2.Text);

UPDATE 1

string firstName = "FirstName: " + txtFirstName.Text;
string lastName = "LastName: " + txtLastName.Text;
string personAddress = "FirstName: " + txtAddress.Text;
string details = firstName + "\n" + lastName + "\n" + personAddress;
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + details);

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98740

Concatenate this two texbox then;

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + Environment.NewLine + tB2.Text );

Upvotes: 1

Related Questions