Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop an windows form application which accepts the input data like the data entered in textboxes, radio buttons selected etc., and save that data into a text file. It worked but the data is not appending in that file,it is replacing.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        private void button1_Click(object sender, EventArgs e)
        {
            StreamWriter ws = new StreamWriter("C:\\File.txt");

            //sw.Write(String.Join(Environment.NewLine, listBox1.Items));
            //textbox
            ws.Write(String.Join(Environment.NewLine,"Name : "));
            ws.WriteLine(String.Join(Environment.NewLine,textBox1.Text));

            ws.WriteLine(String.Join(Environment.NewLine,"Responses are "));

            //q1
            ws.WriteLine(String.Join(Environment.NewLine,"For q1 : "));
            if (radioButton1.Checked)
            {
                ws.Write(String.Join(Environment.NewLine,radioButton1.Text));
            }
            else
            {
                ws.Write(String.Join(Environment.NewLine,radioButton2.Text));
            }
            ws.WriteLine(String.Join(Environment.NewLine,"\n"));

            //q2
            ws.WriteLine(String.Join(Environment.NewLine,"For q2"));
            if (radioButton3.Checked)
            {
                ws.Write(String.Join(Environment.NewLine,radioButton3.Text));
            }
            else if (radioButton4.Checked)
            {
                ws.Write(String.Join(Environment.NewLine,radioButton4.Text));
            }
            else
            {
                ws.Write(String.Join(Environment.NewLine,radioButton5.Text));
            }
            ws.WriteLine(String.Join(Environment.NewLine,"\n"));



            //saving
            ws.Close();
            MessageBox.Show("saved.");
        }
    }
}




here's the screenshot:
http://s0.uploads.im/QLZ7W.jpg[^]
Posted
Updated 26-Mar-14 21:37pm
v6

1 solution

Use File.AppendText:

StreamWriter ws = File.AppendText("C:\\File.txt");


or

Use another overload of the StreamWriter constructor[^]:

StreamWriter ws = new StreamWriter("C:\\File.txt", true);
 
Share this answer
 
Comments
Gouthamitks 27-Mar-14 9:52am    
Thank you BotCar. It worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900